azphare
azphare

Reputation: 525

C++ Templates Question

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?

Upvotes: 0

Views: 129

Answers (3)

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 506975

An expression doesn't only have a value and a type, but it also has a value category. This category can be

  • An lvalue: These expressions generally refer to declared objects, references, functions or dereference results of pointers.
  • An xvalue: These are the result of generating an unnamed rvalue reference. Rvalue references are created by T&& instead of T&. They are a C++11 concept, and you can ignore them here. Mentioned only for sake of completeness.
  • An prvalue: These are the results of casts to non-reference types (like A(10)) or computing/specifying a value, like 42 or 2 + 3.

An lvalue reference requires an lvalue expression for initialization. That is, the following is invalid:

A &x = A(10);

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 like 10 don'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 compilers

int &x = 10; // makes totally no sense

But 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:

// lifetime of the temporary object is lengthened
A const& x = A(10); 

// lifetime of the automatically created temporary object is lengthened
int const& x = 10; 

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& parameter type rather than a A& parameter type.

Upvotes: 7

K-ballo
K-ballo

Reputation: 81349

a+b returns a temporary, if you were allowed to catch a non-const reference to it you would be able to change it and then what? The temporary goes out of scope, and the changes done to it can never be captured by the application. In C++03 temporaries will be bound to const references types.

By the way, this has nothing to do with templates. Rewrite your example to use straight 'A's and you will observe the same behavior.

Upvotes: 3

GManNickG
GManNickG

Reputation: 503855

(a + b) returns a temporary object, though, and can therefore only be bound to a constant reference.

Upvotes: 7

Related Questions