Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a const reference better than pass-by-value in C++11?

Tags:

c++

c++11

I have some pre-C++11 code in which I use const references to pass large parameters like vector's a lot. An example is as follows:

int hd(const vector<int>& a) {    return a[0]; } 

I heard that with new C++11 features, you can pass the vector by value as follows without performance hits.

int hd(vector<int> a) {    return a[0]; } 

For example, this answer says

C++11's move semantics make passing and returning by value much more attractive even for complex objects.

Is it true that the above two options are the same performance-wise?

If so, when is using const reference as in option 1 better than option 2? (i.e. why do we still need to use const references in C++11).

One reason I ask is that const references complicate deduction of template parameters, and it would be a lot easier to use pass-by-value only, if it is the same with const reference performance-wise.

like image 989
thor Avatar asked Jul 03 '14 00:07

thor


People also ask

When should you use a const parameter passed by reference?

Pass Using Const Reference in C++ Now, we can use the const reference when we do not want any memory waste and do not change the variable's value. The above code will throw a compile error as num = num +10 is passed as a const reference.

Why is it usually better to pass objects by reference than by value?

The reason is simple: if you passed by value, a copy of the object had to be made and, except for very small objects, this is always more expensive than passing a reference.

What does pass by const reference mean?

Passing By Reference To Const in C++ | QuantStart. Passing By Reference To Const in C++ Passing By Reference To Const in C++ C++ is an example of a message-passing paradigm language, which means that objects and values are passed to functions, which then return further objects and values based on the input data.

Which is better pass by value or pass by reference?

Pass-by-references is more efficient than pass-by-value, because it does not copy the arguments. The formal parameter is an alias for the argument. When the called function read or write the formal parameter, it is actually read or write the argument itself.


2 Answers

The general rule of thumb for passing by value is when you would end up making a copy anyway. That is to say that rather than doing this:

void f(const std::vector<int>& x) {     std::vector<int> y(x);     // stuff } 

where you first pass a const-ref and then copy it, you should do this instead:

void f(std::vector<int> x) {     // work with x instead } 

This has been partially true in C++03, and has become more useful with move semantics, as the copy may be replaced by a move in the pass-by-val case when the function is called with an rvalue.

Otherwise, when all you want to do is read the data, passing by const reference is still the preferred, efficient way.

like image 151
Rapptz Avatar answered Sep 22 '22 15:09

Rapptz


There is a big difference. You will get a copy of a vector's internal array unless it was about to die.

int hd(vector<int> a) {    //... } hd(func_returning_vector()); // internal array is "stolen" (move constructor is called) vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8}; hd(v); // internal array is copied (copy constructor is called) 

C++11 and the introduction of rvalue references changed the rules about returning objects like vectors - now you can do that (without worrying about a guaranteed copy). No basic rules about taking them as argument changed, though - you should still take them by const reference unless you actually need a real copy - take by value then.

like image 26
cubuspl42 Avatar answered Sep 19 '22 15:09

cubuspl42