c++ - Function initialization with int& argument by int&& -
i have next code:
#include <type_traits> struct ttype { int = 0; bool operator() (int&) { homecoming true; } }; int main() { static_assert(std::is_same<decltype(std::declval<ttype>()(std::declval<int>())), bool>::value, "wtf?"); homecoming 0; }
if seek compile g++-4.8.2 receive error:
main.cpp:321:82: error: no match phone call ‘(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 argument 1 ‘int’ ‘int&’
i don't understand note: no known conversion argument 1 ‘int’ ‘int&’
line. question is: why g++ interprets homecoming type of std::declval<int>()
int
, not line int&&
though std::declval declaration looks like:
template< class t > typename std::add_rvalue_reference<t>::type declval();
i understand it's prohibited bind int&&
int
. why compiler doesn't print: note: no known conversion argument 1 ‘int’ ‘int&’
line. may don't understand , compiler changes somehow homecoming type of std::declval<int>()
int&&
on int
in std::declval<ttype>()(std::declval<int>())
?
thank help!
the problem is, cannot bind xvalue non-const lvalue reference.
let's @ expression
std::declval<int>()
the homecoming type of std::declval<int>
indeed int&&
. hence above look xvalue look of type int
. note in c++ look never has reference type.
but operator
bool operator() (int&)
takes argument non-const lvalue referenece. if alter perameter type const int&
, i.e.
bool operator() (const int&)
everything should work fine.
c++ c++11
No comments:
Post a Comment