Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ variadic template: typeid of it, way to optimize

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...});
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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});
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading