I'm going through r-value references and move semantics, currently, and I'm seeing an odd discrepancy in my own coding experimentation.
Given the below code:
#include <iostream>
using namespace std;
class X{};
void g(X&& t) // A
{
cout << "Rref call" << endl;
}
void g(X& t) // B
{
cout << "Lref call" << endl;
}
template<typename T>
void f(T&& t)
{
g(forward<T>(t));
}
int main()
{
X x;
f(x); // 1
f(X()); // 2
return 0;
}
Will generate the expected output here:
Lref call
Rref call
However, if I go ahead and delete the overloaded function g that takes an r-value reference (denoted above on comment line // A), I get the following output:
Lref call
Lref call
How does this work out? Why doesn't the compiler complain about a call to g(X& t)
while trying to pass in a X&&
?