Given a function
template<typename F> void foo(F func)
I end up with 4 possible situations:
(a) F is a pointer to a binary function
(b) F is a pointer to a ternary function
(c) F is a binary function object type
(d) F is a ternary function object type
Fortunately, I know the result- and parameter type of F, so for the first 3 situations I can use (in foo):
1 2 3 4
|
{
execute_function(func, param1, param2, param3);
...
}
|
...if I provide "execute_function" as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
template<typename T> T execute_function(T&(*fp)(T&, T&), T& arg1, T& arg2, T& arg3)
{
return fp(arg1, arg2);
}
template<typename T> T execute_function(T&(*fp)(T&, T&, T&), T& arg1, T& arg2, T& arg3)
{
return fp(arg1, arg2, arg3);
}
template<typename FunT, typename T> T execute_function(FunT fun, T& arg1, T& arg2, T& arg3)
{
return fun(arg1, arg2);
}
|
...now the overload resolution will select the correct calling mechanism.
However, in the last case, i.e. when a function object is provided, I can either assume a binary- or a ternary function pointer type. (How) is it possible to distinguish between them?
[Note: I know of the possibility to require F to be of type std::binary_function, having a parameter "no_args" et cetera, but I'm seeking a generic solution which doesn't rely on any property of F except that it is a Binary Function Type or Ternary Function Type. The same goes with traits classes - they are a last resort, but I'd like a solution not relying on them]