HACKER Q&A
📣 speedylight

How come pass-by-reference is not the default in C++?


I recently learned the concept of pass-by-reference where instead of copying data into a function’s parameter, you simply give it the address of where the variable being passed is located in memory.

In C++ you have to explicitly define that you want to pass by reference instead of value. Would it not be more efficient to use pass by reference by default since you’re not duplicating data in memory potentially for no real reason?


  👤 dyingkneepad Accepted Answer ✓
There's no "pass-by-reference", never_has_been.jpg. With pass-by-reference what you're actually passing is a value, of a pointer. So you're "duplicating" a value: of the pointer.

What's the point of passing a pointer to an integer instead of passing the integer directly? You're just creating indirection and pointer-walking, which makes your code strictly worse.

Passing by value also prevents the function from having accidental side-effects of changing stuff outside it.

Passing by value allows you to save memory when passing things smaller than a pointer (like, say, an int in x86_64).

Every C/C++ programmer should learn assembly. It makes C trivial to learn, and would certainly have helped you analyze the reasoning behind stuff like this.


👤 ksherlock
A large number of parameters are scalars -- ints, chars, floats, pointers, etc. automatically passing them by reference would be terrible for all the reasons. For OOP, the most important parameter -- this -- is automatically passed by reference (essentially).

(I've heard a tale of a language/compiler (Fortran?) where everything was passed by reference so if you called function(5), it might set 5 (in a literal pool) to some other number...)

Many automatically pass-by-reference languages pass scalars by value so sometimes a parameter is a reference, sometimes it isn't. With C++ and typedefs, it's not always immediately obvious what's a class or a struct or an enum or a scalar.

As it is, for static/inline functions, C++ compilers can analyze the code and convert pass by value to pass by reference if it's appropriate.

Edit --

In most modern ABIs, small structs/classes are passed in registers or on the stack so there's little overhead. Large structs/classes are passed by pointer and then a new copy would be instantiated in the function. With inline constructors and functions (which would be the case for most everything in the STL), a reasonably good compiler could eliminate the copy constructor and use the pointer directly for read-only/const access.


👤 LorenDB
It's all about backwards compatibility. C++ was originally a modification to C, which does not have the concept of references. In fact, while C++ was created in 1979, references were not added until 1985, so there's no way that they could have been the default anyway.

There's no way that it will be changed, either, as changing language behavior would break tons of production code.


👤 wmf
Pass by value is safer because you're less likely to accidentally modify data outside the scope of your function.

👤 iExploder
Sometimes pass by value is what you need, for example passing a shared pointer to get the increment on use count.

Also I believe passing types smaller than or equal to pointer size by reference is redundant.