Search code examples
c++c++17variadic-templatesvariant

C++17, making a variadic template that uses an std::variant that is dependant on template params?


Is there a way, in C++17, to specify a variant where each entry in the variant has been modified from a variadic template in some repeated way?

In particular, I want to declare a template that may look something like this, declaring an std::variant member, something like:

template<typename... Args> class Contains {
      ...
    std::variant<...> choices;
      ...
}

Such that, for example, Contains<Type1, Type2, Type3,....> would have its choices member be std::variant<const Type1 *, const Type2 *, const Type3 *, ....>. where the supplied types may not necessarily share a common base class. That is, each supplied type to the template becomes a const pointer in the std::variant.

I know that I could potentially manually specify each entry passed to the template as the necessary pointer type directly, I am wondering if there is some way to use variadic templates to make the declaration easier to enter using only the names of base types for the Contains template?


Solution

  • Simply do:

    template <typename... Args>
    class Contains
    {
        std::variant<const Args*...> choices;
    };