Search code examples
c++templatesvariadic-templatestypeid

How to fill a vector with typeid's from variadic template arguments


std::vector<std::type_index> vec;

template <typename T1, typename... Tn>
void Fill() {
    vec.push_back(typeid(T1));
    // fill the vector with the remaining type ids
}

I want to fill the vector with the typeids of the template arguments. How could I implement this?


Solution

  • The following solution uses intializer list:

    template <typename... Types>
    void Fill(std::vector<std::type_index>& vec)
    {
        vec.insert(vec.end(), {typeid(Types)...});
    }
    

    See live example.