EDIT -- Found an answer here.
In C++, I am trying to make a functor that I can hand specific variables to use as its function's arguments when I construct it. It uses a variadic template to pass in the argument types and return type of the function pointer. Then it uses a variadic constructor to get the argument values it should use. In code:
template <typename Return, typename... Args>
class Functor {
public:
Functor(Return(*func)(Args...), Args... args) :
function(func), arguments(args) {}
// This overload to make the class "quack like a function"
Return operator()() { return (*function)(arguments); }
private:
Return(*function)(Args...);
Args... arguments; // <-- Right here is the problem. I don't
// believe this is possible.
}
And how it would be used:
#include <iostream>
float add(int x, int y) { return x + y; }
int main() {
// Template arguments: returns float, takes two ints
Functor<float, int, int> func(&add, 2, 3);
std::cout << func() << std::endl; // <-- Should print 5, make sense?
}
function
has an unknown number of arguments until the functor is instantiated, but I think they're guaranteed to match the function signature, because I used the same variadic template to create them both (meaning it wouldn't compile if the function signature didn't match the template arguments). So, how do I store arguments
so that I can pass them to function
later?