The code below is rejected by VC++ 2012 with “error C2207: ‘A::bar’ : a member of a class template cannot acquire a function type”.
int Hello(int n)
{
return n;
}
template<class FunctionPtr>
struct A
{
A(FunctionPtr foo)
: bar(foo)
{}
FunctionPtr bar;
};
int main()
{
A<decltype(Hello)> a(Hello);
return 0;
}
Why?
gcc is a bit more friendly regarding this error :
The simplest solution is to declare
baras a function pointer :In this case,
decltype(Hello)evaluates toint(int)notint(*)(int).