There seem to be two ways (actually 3 ways) to call a function with unknown arguments with or without a return value. One is using a just-in-time compiler, the other is using C++11. In the latter case you can use boost too. I am not really familiar with both approaches. With respect to boost, and reading up, the following code
#define _BOOST_
#ifdef _BOOST_
#include <vector>
#include <iostream>
#include <functional>
#include <stdexcept>
#include <string>
#include <boost/any.hpp>
template <typename Ret, typename... Args>
Ret callfunc (std::function<Ret(Args...)> func, std::vector<boost::any> anyargs);
template <typename Ret>
Ret callfunc (std::function<Ret()> func, std::vector<boost::any> anyargs)
{
if (anyargs.size() > 0)
throw std::runtime_error("oops, argument list too long");
return func();
}
template <typename Ret, typename Arg0, typename... Args>
Ret callfunc (std::function<Ret(Arg0, Args...)> func, std::vector<boost::any> anyargs)
{
if (anyargs.size() == 0)
throw std::runtime_error("oops, argument list too short");
Arg0 arg0 = boost::any_cast<Arg0>(anyargs[0]);
anyargs.erase(anyargs.begin());
std::function<Ret(Args... args)> lambda = ([=](Args... args) -> Ret { <--------------------------
return func(arg0, args...);
});
return callfunc (lambda, anyargs);
}
template <typename Ret, typename... Args>
std::function<boost::any(std::vector<boost::any>)> adaptfunc (Ret (*func)(Args...)) {
std::function<Ret(Args...)> stdfunc = func;
std::function<boost::any(std::vector<boost::any>)> result =
([=](std::vector<boost::any> anyargs) -> boost::any { <-----------------------------
return boost::any(callfunc(stdfunc, anyargs));
});
return result;
}
#endif
would do the trick, yet Xcode complains about “[=]” (see arrows), saying it expects an expression. Perhaps this is the wrong place here on the forum. But any help would be highly appreciated.