Here is a code snippet from a post at Functional C++ blog, describing how a generalized function evaluation can be implemented.
My question is how can you declare template function pointer f like R(C::*f)() with no arguments and still be able to call it with Argsā¦?
// functions, functors, lambdas, etc.
template<
class F, class... Args,
class = typename std::enable_if<!std::is_member_function_pointer<F>::value>::type,
class = typename std::enable_if<!std::is_member_object_pointer<F>::value>::type
>
auto eval(F&& f, Args&&... args) -> decltype(f(std::forward<Args>(args)...))
{
return f(std::forward<Args>(args)...);
}
// const member function
template<class R, class C, class... Args>
auto eval(R(C::*f)() const, const C& c, Args&&... args) -> R
{
return (c.*f)(std::forward<Args>(args)...);
}
template<class R, class C, class... Args>
auto eval(R(C::*f)() const, C& c, Args&&... args) -> R
{
return (c.*f)(std::forward<Args>(args)...);
}
// non-const member function
template<class R, class C, class... Args>
auto eval(R(C::*f)(), C& c, Args&&... args) -> R
{
return (c.*f)(std::forward<Args>(args)...);
}
// member object
template<class R, class C>
auto eval(R(C::*m), const C& c) -> const R&
{
return c.*m;
}
template<class R, class C>
auto eval(R(C::*m), C& c) -> R&
{
return c.*m;
}
struct Bloop
{
int a = 10;
int operator()(){return a;}
int operator()(int n){return a+n;}
int triple(){return a*3;}
};
int add_one(int n)
{
return n+1;
}
int main()
{
Bloop bloop;
// free function
std::cout << eval(add_one,0) << "\n";
// lambda function
std::cout << eval([](int n){return n+1;},1) << "\n";
// functor
std::cout << eval(bloop) << "\n";
std::cout << eval(bloop,4) << "\n";
// member function
std::cout << eval(&Bloop::triple,bloop) << "\n";
// member object
eval(&Bloop::a,bloop)++; // increment a by reference
std::cout << eval(&Bloop::a,bloop) << "\n";
return 0;
}
For instance, when I try:
struct Bloop
{
int a = 10;
int operator()(){return a;}
int operator()(int n){return a+n;}
int triple(){return a*3;}
int foo(int n) {return n;}
};
template <typename R, typename C, typename... Args>
void eval (R(C::*func)(), C& c, Args... args) {
(c.*func)(args...);
}
int main()
{
Bloop bloop;
eval(&Bloop::foo, bloop, 5);
return 0;
}
I get this error:
main.cpp: In function 'int main()':
main.cpp:27:31: error: no matching function for call to 'eval(int (Bloop::*)(int), Bloop&, int)'
eval(&Bloop::foo, bloop, 5);
^
main.cpp:27:31: note: candidate is:
main.cpp:19:6: note: template<class R, class C, class ... Args> void eval(R (C::*)(), C&, Args ...)
void eval (R(C::*func)(), C& c, Args... args) {
^
main.cpp:19:6: note: template argument deduction/substitution failed:
main.cpp:27:31: note: candidate expects 1 argument, 2 provided
eval(&Bloop::foo, bloop, 5);
^
And if I declare func
like R(C::*func)(int)
, it compiles.
The code in the blog article is incorrect (or at least incomplete); it only works for no-argument functions. You could write
eval
more correctly like this:Note the
T...
parameter pack for arguments to the pointer to member function type. This is a distinct type pack fromArgs&&...
because the two packs could be deduced differently.