c++ - unable to determine the template type even it is passed in -
this question has answer here:
i have following code:
template <typename t> struct data { struct embed { t t; }; }; struct functor { template <typename t> void foo( typename data<t>::embed & e) {} }; template <typename t, typename f> struct caller { f f; template <typename t> void invoke() { typename data<t>::embed e; f.foo<t>(e); //compiler error pointed line } }; then specialized template as:
caller<int, functor> c; c.invoke(); compiler error : error: expected primary-expression before '>' in f.foo<t>(e); line. seems compiler doesn't know t specified in template declaration on function.
take out explicit specified t in foo.invoke(e) line result could not deduce template parameter 't'
how fix this? (i still want keep functionality caller can have generic functor , functor's function can templated).
you using:
template <typename t> void invoke() { typename data<t>::embed e; f.foo<t>(e); //compiler error pointed line } inside caller though t 1 of parameters of caller. remove line
template <typename t> and use just:
void invoke() { typename data<t>::embed e; // f.foo<t>(e); //compiler error pointed line f.template foo<t>(e); // need template keyword here. } however, @nawaz pointed out in comment, changes semantics of invoke. if t in invoke meant different t used instantiate caller, better use different name, u.
Comments
Post a Comment