c++ - with template constructor,template argument deduction/substitution failed,why? -


such codes:

template<typename t> struct st{ st(); st(t t){} }; template<typename t> void fun(st<t> t, t a){ }  int main(int argc, char * argv[]) {   st<int> t=2;    fun(2,2); } 

compile g++ 4.8.2 errinfo:

no matches fun(int,int)

candidate is:

template void fun(st, t)

template argument deduction/substitution failed:

mismatched types ‘st’ , ‘int’

try:

template<class t>struct identity{typedef t type;}; template<class t>void fun(typename identity<s<t>>::type s, t t) 

this block compiler trying argument type deduction on first argument.

template type matching pattern matching. few conversions done, in general case cannot solved (is there t such x<t> can converted type z turing-complete arbitrary limits of template compilers: recursion depth etc).

in c++11 can do:

template<class t>struct identity{typedef t type;}; template<class t>suing identity_t = typename identity<t>::type; template<class t>void fun(identity_t<s<t>> s, t t) 

which find cleaner (moves boilerplate away other code).


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -