0
#include <string>
#include <variant>

int main()
{
    using variant_base = std::variant< int, double >;

    using variant_derived_and_flattened = std::variant< std::string, variant_base >;
    // the above does not produce the desired
    // std::variant< int, double, std::string >
    static_assert( std::is_same< variant_base, variant_derived_and_flattened >{} );
}

The static_assert fails.

I assume there is no way to do this and would appreciate a confirmation of this, but perhaps someone knows how to do this?

Grant Rostig
  • 473
  • 1
  • 4
  • 13

1 Answers1

6

You can create a meta-function to transform the variant type.

template <class V, class T> struct variant_append_helper;

template <class... A, class T>
struct variant_append_helper<std::variant<A...>, T> {
    using type = std::variant<A..., T>;
};

template <class V, class T>
using variant_append = typename variant_append_helper<V, T>::type;

static_assert(std::is_same<std::variant<int, double, std::string>,
                           variant_append<std::variant<int, double>, std::string>>{});
Brian Bi
  • 111,498
  • 10
  • 176
  • 312