I have following code:
#include <type_traits>
struct TType
{
int a = 0;
bool operator() (int&)
{
return true;
}
};
int main()
{
static_assert(std::is_same<decltype(std::declval<TType>()(std::declval<int>())), bool>::value, "wtf?");
return 0;
}
If I try to compile it with g++-4.8.2 then I receive an error:
main.cpp:321:82: error: no match for call to ‘(JetPlane) (int)’
static_assert(std::is_same<decltype(std::declval<JetPlane>()(std::declval<int>())), bool>::value, "wtf?");
^
main.cpp:265:8: note: candidate is:
struct JetPlane
^
main.cpp:279:7: note: bool JetPlane::operator()(int&)
bool operator() (int&)
^
main.cpp:279:7: note: no known conversion for argument 1 from ‘int’ to ‘int&’
I don't understand note: no known conversion for argument 1 from ‘int’ to ‘int&’ line. So the question is: why g++ interprets return type of std::declval<int>() like int and not line int&& though std::declval declaration looks like:
template< class T >
typename std::add_rvalue_reference<T>::type declval();
I understand that it's prohibited to bind int&& to int. But why then compiler doesn't print: note: no known conversion for argument 1 from ‘int’ to ‘int&’ line. May be I don't understand something and compiler changes somehow return type of std::declval<int>() from int&& on int in std::declval<TType>()(std::declval<int>())?
Thank you for help!
The problem is, that you cannot bind an xvalue to a non-const lvalue reference.
Let's look at the expression
std::declval<int>()
The return type of std::declval<int> is indeed int&&. Hence the above expression is an xvalue expression of type int. Note that in C++ an expression never has a reference type.
But your operator
bool operator() (int&)
takes its argument by non-const lvalue referenece. If you change the perameter type to const int&, i.e.
bool operator() (const int&)
everything should work fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With