0
template<typename... Ts>
using Type = std::variant<std::shared_ptr<T1>, std::shared_ptr<T2>, std::shared_ptr<Tn>>;

How I can do this using variadic templates and fold expressions?

I try something like this:

template<typename... Ts>
using Type = std::variant<((std::shared_ptr<Ts>), ...)>;`

but it doesn't compile.

Deratelo
  • 13
  • 2
  • 1
    Please don't guess your way around C++. Read a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) instead. – Passer By May 02 '23 at 13:12
  • 1
    Always post error along with your code, always. – Jason May 02 '23 at 13:13
  • `template using Type = std::variant...>;` ? – Jason May 02 '23 at 13:17
  • 1
    Don't mix variadic templates and fold expressions. They are completely different things. In your case no fold expression is needed. – Evg May 02 '23 at 13:18

1 Answers1

2

The following compiled for me

template<typename... Ts>
using Type = std::variant<std::shared_ptr<Ts>...>;

As stated here

A pattern followed by an ellipsis, in which the name of at least one parameter pack appears at least once, is expanded into zero or more comma-separated instantiations of the pattern, where the name of the parameter pack is replaced by each of the elements from the pack, in order

Here our pattern is std::shared_ptr<Ts>, which is expanded into std::shared_ptr<T1>, std::shared_ptr<T2>, std::shared_ptr<Tn>

Karen Baghdasaryan
  • 2,407
  • 6
  • 24