I have a class that executes a member function in its destructor, to ensure the functionality is performed when abandoning the scope. It can take a parameter of any type (as seen below), but I want to extend it so it can receive any number of parameters and any type of parameters (sounds like a candidate for variadic templates). However how does one store this variable number/type of parameters for use when one invokes the member function?
This is what I have so far (only one parameter supported):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
template<typename T, typename P, typename Ret = void>
struct Defer {
using PMF_P = Ret (T::*)(P par);
Defer( T& t, PMF_P fnobj, P par ) : obj{t}, fn{fnobj}, parameter{par} {}
~Defer() {
(obj.*fn)(parameter);
}
private:
T& obj;
PMF_P fn;
P parameter;
};
Yes, you might do that in this particular case ... however in a more general case, where you receive variable number of parameters which you want to use later in a call to a method, how can one store them for later use?
Its really beyond the original question ... a bit more ambitious