I'm trying to create a class which will store pointer to function with variable number of arguments, and call it later.
The idea is to create a wrapper for function that will call said function when the object destructs. That way I can for example ensure some cleanup happens after exiting some function.
What I have now is a little modified code written by Faheem Mitha posted here.
Here is the working code with example (I'm compiling this with Visual Studio 2015):
#include "stdafx.h"
#include <tuple>
#include <iostream>
using namespace std;
template<int...> struct seq {};
template<int N, int... S> struct gens : gens<N - 1, N - 1, S...> {};
template<int... S> struct gens<0, S...> { typedef seq<S...> type; };
template<typename Return, typename... Args> class CallOnExit
{
Return(*_func)(Args...);
std::tuple<Args...> _args;
template<int... S> Return call_func(seq<S...>)
{
return _func(std::get<S>(_args)...);
}
Return call()
{
return call_func(typename gens<sizeof...(Args)>::type());
}
public:
CallOnExit(Return(*func)(Args...), Args&&... args)
{
_func = func;
_args = std::forward_as_tuple(args...);
}
~CallOnExit()
{
if (_func != nullptr)
call();
}
};
void f(int i, double d)
{
cout << i << endl << d << endl;
}
int main()
{
{
CallOnExit<void, int, double> c(f, 1, 2);
}
cin.get();
return 0;
}
The problem is I have to make this work on Arduino platform where stl is not available (so no std::tuple, no std::forward, no std::get). C++11 is supported on Arduino.
What is the minimal amount of work required to make this example work without stl?