I have written the following short code in C++11 of a variable template function and store the arguments into a vector of type boost::any. It is working perfectly, but I don't want to use the boost::any library (due to some limitation).
#include <boost/any.hpp>
template <class Var, class... Args>
void cpp_for(Var *variable, uint32_t numParams, Args... args)
{
std::vector<boost::any> arguments{args...};
if(arguments.size() != numParams)
throw std::runtime_error("mismatch");
for(uint32_t i = 0; i < numParams; ++i)
variable[i] = *(boost::unsafe_any_cast<Var>(&arguments[i]));
}
And I call the function like this:
cpp_for(myObj->var, 3, 0x56, 0x23, 0x10);
Or
cpp_for(myObj2->var, 2, myObj2->var2, myObj2->var3);
Is there any way to store the arguments and process them one by one without the need for boost::any?
Edit 1: my arguments are all of the same type.
Edit 2: Since the goal of the code above is assignment
, then creating an extra data structure (vector) is useless. Check 'Nir Friedman''s answer for a more efficient solution.