Can someone explain the output of the following code?
#include <iostream>
template <class T>
void assign(T& t1, T& t2){
std::cout << "First method"<< std::endl;
t1 = t2;
}
template <class T>
void assign(T& t1, const T& t2) {
std::cout << "Second method"<< std::endl;
t1 = t2;
}
class A
{
public:
A(int a) : _a(a) {};
private:
friend A operator+(const A& l, const A& r);
int _a;
};
A operator+(const A& l, const A& r)
{
return A(l._a + r._a);
}
int main ()
{
A a = 1;
const A b = 2;
assign(a, a);
assign(a, b);
assign(a, a + b);
}
The output is
First method
Second method
Second method
I don’t see why. Shouldn’t the last call to assign activate the first version, since (a+b) doesn’t return a const A object?
An expression doesn’t only have a value and a type, but it also has a value category. This category can be
T&&instead ofT&. They are a C++11 concept, and you can ignore them here. Mentioned only for sake of completeness.A(10)) or computing/specifying a value, like42or2 + 3.An lvalue reference requires an lvalue expression for initialization. That is, the following is invalid:
The reason behind this is that only lvalue expressions refer to things that are suitable and intended for staying alive a longer time than only for the duration of the initialization. Like, a declared object is alive until exiting its block (if it was a local non-static variable) or until the end of the program (if it was declared outside functions and classes). The rvalue expression
A(10)refers to an object that dies already when the initialization is finished. And if you said the following, it would not make any sense of all, because pure values like10don’t have an address at all, but references require some sort of identity to which they bind, which in practice is implemented by taking the address of their target internally in compilersBut for const references, C++ has a backdoor. When initialized with a prvalue, a const lvalue reference will automatically lengthen the lifetime of the object, if the expression refers to an object. If the expression has a non-object value, C++ creates a temporary object with a value of that expression, and lengthens the lifetime of that temporary, binding the reference to that temporary:
What happens in your case?
Now the compiler in your case, because you supply a temporary object, will choose the version that has a
A const¶meter type rather than aA¶meter type.