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

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -