Is there a way to allow a template parameter of function pointer type to accept a function of any (rather than a specific) return type, when the function's return value is not actually used? Here's an MCVE to illustrate what I mean:
int returnInt(int) { return 0; }
void returnVoid(int) { }
template <int (*Func)(int)>
struct foo { void bar(int x) { Func(x); } };
int main(int, char *[]) {
foo<returnInt> a; // ok
foo<returnVoid> b; // argument of type "void (*)(int)" is incompatible
// with template parameter of type "int (*)(int)"C/C++(458)
}
I know I can do this, as a workaround:
template <typename ReturnType, ReturnType (*Func)(int)>
struct foo { void bar(int x) { Func(x); } };
int main(int, char *[]) {
foo<int, returnInt> a; // ok
foo<void, returnVoid> b; // ok
}
Or even this (which is worse in my opinion, because we lose the type check on the function type's argument types and could fall into SFINAE):
template <typename FuncType, FuncType *Func>
struct foo { void bar(int x) { Func(x); } };
int main(int, char *[]) {
foo<decltype(returnInt), returnInt> a; // ok
foo<decltype(returnVoid), returnVoid> b; // ok
}
But I wonder if there's a way to do it without adding an extra template parameter which has no other purpose.