3 ms·
when you read a function call foo(bar, baz, bajinga); Which of those are pass by value, by reference or by const reference? I say it would be useful to kn
by harry8 7y ago
when you read a function call
foo(bar, baz, bajinga);
Which of those are pass by value, by reference or by const reference? I say it would be useful to know that by inspection from just reading that line of code. You don't have to agree. And you care about by value vs by reference because copy constructors exist and you need to reason about performance or if you don't need to why are you using c++ anyway?
You mostly care whether it's by value or by reference because what is passed could be altered with the latter unless it's marked const. But which is it? Stop! Stop what you're reading right now and go and look at the foo() signature. 9 times in 10 you won't be surprised, the arguments should not be altered by the function call but you might want to check that, then get back to whatever you were actually trying to understand with a break in the flow of your understanding and the prospect of some context fading slightly from your short term memory.
But yeah some people are so smart that there is literally nothing in coding readability they need and sneer at those of us who take a different approach. Such people never make mistakes. Twas ever thus, well before C++.
- nayuki 7y agoRust is a relatively new programming language that features nearly all the good parts of C++ (e.g. struct, reference, RAII, move semantics, generics) and almost none of the bad parts (e.g. duplicate features, cryptic syntax, undefined behavior). Your example in Rust would read like this: // Definition fn foo(a: u8, b: &u8, c: &mut u8) -> i32 { a = 0; // Local change only println!("{}", *b); // Read pointee *c = 1; // Write pointee return 2; } // Call site let bar: u8 = 3; let baz: u8 = 4; let mut bajinga: u8 = 5; foo(bar, &baz, &mut bajinga);
- steveklabnik 7y agoYou shouldn’t need the type annotations on the three variables, by the way.
- kllrnohj 7y ago> And you care about by value vs by reference because copy constructors exist and you need to reason about performance or if you don't need to why are you using c++ anyway? That's an implementation detail of the function you're calling, not a performance issue at the call site. The call site should not have an opinion on this. Either the object is big enough it should be pass-by-reference, or it's not. You don't need to sprinkle that detail at every call site. > You mostly care whether it's by value or by reference because what is passed could be altered with the latter unless it's marked const. For mutable parameters I agree, those should be passed by pointer. Because they actually have an influence at the call site. But const references, which is what we were talking about, have no behavior change at the call site. So they should not stand out or be marked special, since they aren't. > But yeah some people are so smart that there is literally nothing in coding readability they need and sneer at those of us who take a different approach. If you insist all pass-by-const-refs should be pass-by-pointer then you've also lost your ability to code-style distinguish mutable vs. non-mutable parameters. Which makes it much easier to make mistakes, not harder. And also litters your code base with meaningless, non-behavior-altering noise.