c++ - Template parameter deduction with function pointers and references -
possible duplicate:
why qualifiers of template arguments stripped when deducing type?
consider following c++ code:
void f(int&); template <typename t> void tpl(void (*)(t), t); void bar(int& x) { tpl(&f, x); }
compilation using gcc 4.6.0 fails following error message:
fntpl.cpp: in function ‘void bar(int&)’: fntpl.cpp:7:11: error: no matching function call ‘tpl(void (*)(int&), int&)’ fntpl.cpp:7:11: note: candidate is: fntpl.cpp:3:46: note: template<class t> void tpl(void (*)(t), t)
if state template parameters explicitely (tpl<int&>(&f, x)
), works. why doesn't template argument deduction work in case?
because these fundamentally different
void f(int&);
and
void (*)(t)
the compiler has deduced t
int
, looks for:
void f(int);
which nothing intention, change function pointer this:
template <typename t> void tpl(void (*)(t&), t);
and compiler happy...
Comments
Post a Comment