Possible Duplicate:
Why are qualifiers of template arguments stripped when deducing the type?
Consider the 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 with the following error message:
fntpl.cpp: In function ‘void bar(int&)’:
fntpl.cpp:7:11: error: no matching function for call to ‘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 I state the template parameters explicitely (tpl<int&>(&f, x)), it works. Why doesn’t template argument deduction work in this case?
Because these are fundamentally different
and
the compiler has only deduced that
Tisint, so it looks for:which is nothing like your intention, change the function pointer to this:
And the compiler will be happy…