c++ - Understanding std::forward -
why compiler not able deduce template parameter std::forward?
i mean:
#include <memory> #include <iostream> struct x{}; struct a{ a( const x& ) { std::cout << "cpy ctor\n"; } a( x&& ) { std::cout << "move ctor\n"; } }; x foo() { return {}; } template<typename t,typename arg> t* factory( arg&& ) { return new t(std::forward(a)); // ----------^^^^^^^^^^^^^^^ error: can't deduce template parameter } int main() { factory<a>(foo()); } i know design choice (due std::remove_reference in definition of std::forward) avoid user forget specify type. can't is: why way it's implemented works prevent deduction? why compiler not deducing forward's template parameter arg.
std::forward declared so:
template< class t > t&& forward( typename std::remove_reference<t>::type& t ); typename std::remove_reference<t>::type non-deduced context. compiler has no way of knowing t should deduced because doesn't understand semantic connection between type member type , given t. need search through types find match , able somehow disambiguate collisions. unreasonable, standard doesn't allow it.
Comments
Post a Comment