vendredi 11 août 2017

Types of the arguments in a variadic template function

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). I am thinking of getting the type of arguments (they are all of the same type in my program) and create the vector without the need for boost::any. Is there any way to get the argument type?

#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);

Aucun commentaire:

Enregistrer un commentaire