When passed a parameter, I would like to distinguish between these two cases in a function parameter, like this:
int rvalue();
int&& rvalue_ref();
f(rvalue());
f(rvalue_ref());
However, when I try with forwarding references like this:
int rvalue()
{
return 1;
}
int&& rvalue_ref(int i)
{
return std::move(i);
}
template<class T>
void f(T&& x)
{
if (std::is_rvalue_reference<T>())
{
std::cout << "Rvalue reference" << std::endl;
}
else if (std::is_lvalue_reference<T>())
{
std::cout << "Lvalue reference" << std::endl;
}
else
{
std::cout << "Not a reference" << std::endl;
}
}
int main()
{
f(rvalue()); // Should print "Not a reference"
f(rvalue_ref(1)); // Should print "Rvalue reference"
}
It prints out "Not a reference" for both cases. Is there a way to distinguish both cases in C++?