DEV Community

Discussion on: Pass-By-Value in C++ and Rust

Collapse
 
edubez profile image
Eduardo Bezerra

There is no common pass-by-value bug in C++. This behavior is by design since you have Value and Reference semantics in the language.
C++ is a language where you should express your intent.
If you want to transfer ownership you should use std::move.
If you want to keep ownership and work with the same object you pass by reference to non const.
If you want to work with a copy you pass by value.

Collapse
 
deciduously profile image
Ben Lovy

Sure, it's not a bug with C++, its a bug with the C++ programmer. It's still something that only comes with experience, whereas Rust by default will catch this programmer-introduced mistake for you.

I'm not trying to state this in the general case, only from a beginner's perspective. Both languages have their uses, it's not a cut-and-dry superiority situation.

Collapse
 
rtxa profile image
rtxa

The only difference is that Rust compiler will warn/error you about that and C++ will not for most of the cases (unless you use a static analyzer which will reduce the odds).
In every language you need to learn the rules, otherwise you will fight with the compiler everytime.