meguli
meguli

Reputation: 1526

About passing a temporary object to a const reference

In the book by Andrew Koenig, Accelerated C++, he shows the following code:

vector<double> emptyvec()
{
    vector<double> v;    // no elements
    return v;
}

grade(midterm, final, emptyvec());

Now, grade function takes a const reference to vector<double>. This means to me that the temporary object returned by the emptyvec() should be copied because it is created in the stack as a local variable and aliasing that seems strange to me. How exactly this works? Also on another page, he passes a const reference to a function that takes its arguments by value and modifies them. Can a const reference of vector<double> be passed to a function whose parameter expects vector<double>?

Upvotes: 8

Views: 5485

Answers (2)

eerorika
eerorika

Reputation: 238461

Now, grade function takes a const reference to vector. This means to me that the temporary object returned by the emptyvec() should be copied

The fact that grade accepts a const reference does not influence whether the object returned by emptyvec is copied or not.

How exactly this works?

If you return a local variable by value, then you conceptually make a copy (or a move since C++11) of the local variable.

However, c++ standard allows copies (and moves) to be "elided". In practice, the optimizer can choose to construct the returned object in-place where it would have been copied to. This is called "(Named) Return Value Optimization".

Also on another page, he passes a const reference to a function that takes its arguments by value and modifies them. Can a const reference of vector be passed to a function whose parameter expects vector?

Initializing an object with a (const) reference of same type will invoke the the copy constructor. The argument of the function will be a copy of the object that was referred to.

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727047

This means to me that the temporary object returned by the emptyvec() should be copied because it is created in the stack as a local variable

Absolutely, it does get copied. However, the place to which it gets copied is designated by the compiler, not by your program.

If it were not for compiler's ability to pass a temporary for a constant reference, you would be forced to do this:

vector<double> tmp(emptyvec());
grade(midterm, final, tmp);

That is pretty much what happens behind the scene in the code from the book, except there's no tmp variable accessible to the code.

Can a const reference of vector<double> be passed to a function whose parameter expects vector<double>?

Yes, as long as the parameter is passed by value, not by reference. Recall that a copy constructor takes a const reference. When you call a function that takes vector<double> and pass const vector<double>& to it, C++ invokes a constructor for vector<double>, passes it a constant reference, and uses the resultant copy inside the function.

Upvotes: 5

Related Questions