I am trying to write a template function that accepts a std::function
which depends on the template arguments. Unfortunately the compiler is not capable of corr
You need to make the third function parameter a non-deduced context for the template parameters therein. Then the compiler will not compare the argument type against the parameter type without also considering all implicit conversions (the Standard says, and C++0x clarified this further, that for a function parameter where there are no template parameters in deducing positions, all implicit conversions are allowed in bridging a difference).
template < typename T > struct id { typedef T type; };
template < typename Ident, typename Param >
void CallFunc( Ident ident, Param param,
typename id>::type op )
{
op( ident, param );
}
Instead of id
you can use boost::identity
. In C++0x and compilers that support it, you can have a more readable edition using alias templates
template < typename T > using nondeduced = typename id::type;
Then your code becomes simply
template < typename Ident, typename Param >
void CallFunc( Ident ident, Param param,
std::function< nondeduced > op )
{
op( ident, param );
}
However GCC does not yet support alias templates.