5 ms·
There is no pass-by-value overhead. There are only implementation decisions. Pass by value describes the semantics of a function call, not implementation. Pas
by jklowden 11mo ago
There is no pass-by-value overhead. There are only implementation decisions.
Pass by value describes the semantics of a function call, not implementation. Passing a const reference in C++ is pass-by-value. If the user opts to pass "a copy" instead, nothing requires the compiler to actually copy the data. The compiler is required only to supply the actual parameter as if it was copied.
- duped 11mo agoUnfortunately "the compiler is required to supply the actual parameter as if it was copied" is leaky with respect to the ABI and linker. In C and C++ you cannot fully abstract it.
- mattnewport 11mo agoThis might be true in the abstract but it's not true of actual compilers dealing with real world calling conventions. Absent inlining or whole program optimization, calling conventions across translation units don't leave much room for flexibility. The semantics of pass by const reference are also not exactly the same as pass by value in C++. The compiler can't in general assume a const reference doesn't alias other arguments or global variables and so has to be more conservative with certain optimizations than with pass by value.
- themafia 11mo ago> Passing a const reference in C++ is pass-by-value. I can cast the const away. The implementation does not hide this detail. The semantics therefore must be understood by the programmer.
- Ericson2314 11mo agoYou are thinking "call by value". The author probably used "pass" not "call" specifically to avoid this.
- kazinator 11mo agoThere is no difference. Call-by-alue is the older term, and I believe still preferred in CS acdemia.
- gpderetta 11mo agoI think that call-by-value/call-by-name/call-by-need[1] are more about strict vs lazy evaluation, as opposed to by-value/by-reference semantics. [1] there is also call-by-push-value, but i was never able to wrap my mind around it.