The following code fails to compile when the template parameter T
is a fundamental type such as int
(on gcc 4.8). Is this standard conforming behaviour? My understanding of std::declval
was that it always resolves to either a T&&
or T&
.
template <class T>
void foo(T&& val)
{
std::cout << noexcept(std::declval<typename std::decay<T>::type>() = val);
}
struct bar { };
bar b;
foo(b); // okay
int a;
foo(a); // error: using xvalue (rvalue reference) as lvalue
The error occurs at the point of assigning val
to the std::declval
expression.
It works if I remove the std::decay
and use std::declval<T>
directly, but I'm not sure why. The decayed type should just be int
and so std::declval<int>()
should have a return type of int&&
shouldn't it?