Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



This 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.


Unfortunately "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.


> 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.


You are thinking "call by value". The author probably used "pass" not "call" specifically to avoid this.


There is no difference. Call-by-alue is the older term, and I believe still preferred in CS acdemia.


I 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: