3 ms·
A large number of parameters are scalars -- ints, chars, floats, pointers, etc. automatically passing them by reference would be terrible for all the reasons.
by ksherlock 3y ago
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.