Reputation: 19093
second line compiles well, but third gives me an errors
int (* const f)() = ff;
cout << typeid(replace<int (*)(), int, char>::type).name() << endl;
cout << typeid(replace<f, int, char>::type).name() << endl;
test.cpp:3:25: error: the value of ‘f’ is not usable in a constant expression
test.cpp:1:15: note: ‘f’ was not declared ‘constexpr’
test.cpp:3:37: error: template argument 1 is invalid
Upvotes: 1
Views: 968
Reputation: 507393
A template argument can be a function pointer only if the parameter is a non-type parameter. But sadly C++11 does even not yet allow to use the full variety of constant expressions to compute a template argument. For a non-type pointer or reference, you are only allowed to pass the value of another template parameter or the directly obtained address of the function or object, without storing it first in a const
/ constexpr
variable.
This limitation will most probably be lifted for next C++ revision.
Upvotes: 3
Reputation: 4184
You could use templated interface instead of function pointer:
template<class T>
class IMyInterface{
public:
virtual void doSomething(const T& arg) =0;
};
Upvotes: 0