I know you pass by reference to a function in C++ when you want to change the value of the original variable. But you can also pass by reference when you want the program to be more efficient and if you don t want to change anything in the variable passed to the function, you just make it const. My question is, why not always have your functions accept variables passed by const reference if it is more efficient than just passing the variable and having the compiler create a new one within the scope of the function? To extend the question, what would be a case where a function WOULD need to copy a variable over passed through the parameters?
Function - Why not always pass by const reference in C
Sommaire |
Questions
Answers
When an argument is passed by value it is modifiable and copying it may be elided. For example, the canonical way to implement the assignment operator looks like this:
T& T::operator= (T value) { value.swap(*this); return *this; }
At first sight it may look inefficient because a T is being copied. However, it would be copied anyway, i.e., if a copy is needed one will be created either way:
T& T::operator= (T const& value) { T(value).swap(*this); // has to do a copy right here return *this; }
However, for the first version, it may be possible not to create copy at all, for example
T f() { return T(); } // ... T x = ...; x = f();
When assigning the result of f() which is of type T to x the compiler may decide that it doesn t need to copy the result of f() and instead pass it into the assignment operator directly. In that case, if the assignment operator takes the argument by const& the compiler has to create a copy inside the assignment operator. In the implementation taking the argument by value it can elide the copy! In fact, the return from f() can already elide the copy, i.e., the call to f() and the following assignment may just involve the default construction of the object! ... and for many modern compilers that is, indeed, the case!
Put differently: if you need to copy an argument, getting it passed by value may avoid the need to create a copy. Also, you can std::move() from value arguments but not from const& arguments.
Source
License : cc by-sa 3.0
http://stackoverflow.com/questions/19967958/why-not-always-pass-by-const-reference-in-c