c++ - C++11 templated function with rvalue param call -
in class o, have templated function test2:
struct a{int value;}; struct o{ value; template<typename args> static void test2(args &&args){ std::cout << std::endl << "!!!" << std::is_rvalue_reference<decltype(args)>::value << std::endl; } }; than, want phone call function one:
template<typename args> void test(args &&args){ using t = decltype(std::forward<args>(args).value); std::cout << std::is_rvalue_reference<decltype(args)>::value; std::cout << std::is_rvalue_reference<decltype(std::forward<args>(args).value)>::value; std::cout << std::is_rvalue_reference<t>::value; // ok o.test2(std::forward<args>(args).value); // alvays rvalue, if agrs lvalue o::template test2<t>( std::forward<t>( std::forward<args>(args).value ) ); // nor work @ all, cant cast a&& o::template test2<t>( std::forward<args>(args).value ); ); } http://coliru.stacked-crooked.com/a/3bbf040904845a54
if pass std::forward<args>(args).value without specifying template type, deduce type correctly, if have pass type, how should phone call function than?
it seems can't manually deduce type correctly.
update
i need specify arguments explicitly, because have function (pseudo-code):
//initially phone call wind somewhere. // tuple defined in class , std::tuple template<class callback, class ...args> void wind(tuple&& tuple, callback &&callback){ using elementt = decltype(std::get<index>(std::forward<tuple>(tuple))); /// /// !!! problem here !!! /// callback.template operator()<elementt, args...>( std::get<index>(std::forward<tuple>(tuple)) ); // std::get automatically homecoming &/&& // recursivly phone call wind until end wind<callback, args...>( std::forward<tuple>(tuple), std::forward<callback>(callback)); } // callback looks like: struct callme{ // args provide type info. no function arguments here. template<class data, class ...args> void operator(data &&data){ } } this question related phone call both - wind , callback() functions.
template<typename args> static void test2(args&& args) { ... }
in above function, though parameter type looks rvalue reference, can bind both rvalue , lvalue arguments. colloquially known universal reference. if function argument rvalue of type u, t deduced u, , t&& u&&, rvalue reference, straightforward. however, when function argument lvalue of type u, t deduced u&, means function parameter type u& &&, undergoes reference collapsing become u&. however, these special rules apply, type must deduced.
o::test2(std::forward<args>(args).value); // changed o.test2 o::test2 in case, template parameter type beingness deduced function argument. argument lvalue, type of function parameter args lvalue.
o::template test2<t>( std::forward<t>( std::forward<args>(args).value ) ); the difference here you've explicitly specified template parameter type test2. no deduction taking place, , function parameter simple rvalue reference in case. can bind rvalues, , provide rvalue due outer std::forward<t> cast (here t = a). has same effect static_cast<a&&>(value).
o::template test2<t>( std::forward<args>(args).value ); the lastly case should've explained why doesn't work. explained above, test2 can bind rvalue in case, , without sec std::forward above, you're trying bind lvalue rvalue reference parameter, fails.
c++ templates c++11 rvalue-reference
No comments:
Post a Comment