Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create variant alternating value and array of value

I want to have something like:

template <typename... Ts>
using make_variant_t = /* ??? */;

such that, for instance, make_variant_t<Foo, Bar> evaluates as the type

 std::variant<Foo, std::vector<Foo>, Bar, std::vector<Bar>>

in that order. How, if it is possible, can this be achieved?

like image 221
user877329 Avatar asked Nov 18 '19 17:11

user877329


1 Answers

With Boost.Mp11 this is a short, one-liner (as always):

template <typename... Ts>
using make_variant_t = mp_append<variant<Ts, vector<Ts>>...>;

make_variant_t<int, char> will first produce two variants, variant<int, vector<int>> and variant<char, vector<char>>. Those each are "lists" in the Mp11 sense, and mp_append takes a bunch of lists and concatenates them together, producing variant<int, vector<int>, char, vector<char>> as desired. Demo.

like image 139
Barry Avatar answered Sep 24 '22 12:09

Barry