#include <iostream>
#include <typeinfo>
struct C
{
template<class T>
C(T && t) { std::cout << typeid(T).name() << std::endl; }
};
struct D
{
D(int && t) { }
};
int main()
{
int i = 1;
std::cout << typeid(i).name() << std::endl;
C c(i); // OK
D d(i); // error
}
The line D d(i);
fails to compile with:
foo.cc:22:7: error: cannot bind 'int' lvalue to 'int&&'
However, commenting it out, the generated output is:
i
i
which suggests that T
was deduced as int
. How does D(int &&)
fail to bind but C(int &&)
succeeds?