Home > Back-end >  C variadic template: typeid of it, way to optimize
C variadic template: typeid of it, way to optimize

Time:05-20

So, I learn variadic templates and usage of it. Now I made that code below. The question is does some other methode exist for getting type of "Params" without any arrays or inilialized_list?

template<class Type, class... Params>
void InsertInVector(std::vector<Type>& v, const Params&... params)
{
    const auto variadic = {typeid(Params).name()...};
    if (typeid(Type).name() != *variadic.begin())
    {
        throw std::runtime_error("TYPES ARE NOT THE SAME!");
        return;
    }
    v.insert(v.end(), {params...});
}

CodePudding user response:

In C 17 and later, you can do something like this:

template<class Type, class... Params>
void InsertInVector(std::vector<Type>& v, const Params&... params) {
  static_assert((std::is_convertible_v<Params, Type> && ...));
  v.insert(v.end(), {params});
}
  • Related