c++variadic-templatespack-expansion

How to create a template with variable number of type parameters?


In our codebase we use std::variant<std::shared_ptr<SomeClass>, ...> a lot.

That obviously requires a lot of writing. How to make a template?

template <class... T>
using VarSP = std::variant<std::shared_ptr<???>>;

Where should T go in the above snippet? The desired behavior should be:

VarSP<Foo, Bar, Baz> // std::variant<std::shared_ptr<Foo>, std::shared_ptr<Bar>, std::shared_ptr<Baz>>

Solution

  • template <typename... T>
    using VarSP = std::variant<std::shared_ptr<T>...>;