I’m trying to write a template which gets the type of a functionpointer as templateargument and the corresponding functionpointer as a function argument, so as a simplyfied example I’m doing this right now:
int myfunc(int a)
{ return a; }
template<typename T, typename Func> struct Test
{
typedef typeof(myfunc) Fun;
static T MyFunc(T val, Func f)
{
return f(val);
}
};
int main(void)
{
std::cout<<Test<int, typeof(myfunc)>::MyFunc(5, myfunc)<<std::endl;
}
However this code instantly crashes. If I change the type of f to Fun it works perfectly.
So what am I doing wrong and how can I get this to work?
I’m using mingw under windows vista if that behaviour in case that behaviour is compiler dependent.
You don’t really need the
Testclass for this scenario, and Why use thetypeoffunction here?will do, and no fiddling with function pointer types:
The template arguments are deduced automatically for functions!