Reputation: 145
Some code will help making sense:
#include <variant>
#include <vector>
#include <string>
using MyType = std::variant<int, float, string>;
using ContainerOfMyType = std::vector<MyType>;
using MySuperType = std::variant<MyType, ContainerOfMyType>;
Instead of having MySuperType
being a std::variant
of another std::variant
and a std::vector
of std::variant
, I would like to have all types unpacked into a single std::variant
deriving from MyType
.
So the expected result in that case would be (written manually):
using MySuperType = std::variant<int, float, string,
std::vector<int>, std::vector<float>,
std::vector<string>>;
Is it possible? How can I achieve this?
Upvotes: 0
Views: 511
Reputation: 302757
With Boost.Mp11, this is a short one-liner (as always):
using MySuperType = mp_append<MyType, mp_transform<std::vector, MyType>>;
mp_transform<vector, MyType>
gives you the variant
of vector
s of each type, and then mp_append
concatenates those two together.
Demo.
Upvotes: 1
Reputation: 42746
I would like to declare a new
variant
type that contains all these base types, plusvector
of each of these base types.
Template partial specialization should be enough
#include <variant>
#include <vector>
template<class Var>
struct MySuper;
template<class... Args>
struct MySuper<std::variant<Args...>> {
using type = std::variant<Args..., std::vector<Args>...>;
};
Upvotes: 3