25

I have a std::variant that I'd like to convert to another std::variant that has a super-set of its types. Is there a way of doing it than that allows me to simply assign one to the other?

template <typename ToVariant, typename FromVariant>
ToVariant ConvertVariant(const FromVariant& from) {
    ToVariant to = std::visit([](auto&& arg) -> ToVariant {return arg ; }, from);
    return to;
}

int main()
{
    std::variant<int , double> a;
    a = 5;
    std::variant <std::string, double, int> b;
    b = ConvertVariant<decltype(b),decltype(a)>(a);
    return 0;
}

I'd like to be able to simply write b = a in order to do the conversion rather than going through this complex casting setup. Without polluting the std namespace.

Edit: Simply writing b = a gives the following error:

error C2679: binary '=': no operator found which takes a right-hand operand of type 'std::variant<int,double>' (or there is no acceptable conversion) 

note: while trying to match the argument list '(std::variant<std::string,int,double>, std::variant<int,double>)'
Bomaz
  • 1,871
  • 1
  • 17
  • 22
  • 1
    If `U` is universe of all the types , and P has U, isn't P equals U ? I am having trouble understanding what problem you're trying to solve, may be I'm dumb :| – P0W Nov 09 '17 at 13:38
  • Try compiling `b=a` directly, to see the issue. – Sam Varshavchik Nov 09 '17 at 13:39
  • The error is as follows: error C2679: binary '=': no operator found which takes a right-hand operand of type 'std::variant' (or there is no acceptable conversion) note: while trying to match the argument list '(std::variant, std::variant)' – Bomaz Nov 09 '17 at 13:46
  • `b = ConvertVariant(a);` can be simplified to `b = ConvertVariant(a);` and if `decltype(b)` bothers you, you can have output parameter to your function to have something like `ConvertVariantTo(a, b);`. – Jarod42 Nov 09 '17 at 13:55
  • 1
    why isn't this conversion part of std::variant? If I understand correctly, Boost.Variant does have this functionality... – dats Mar 04 '20 at 14:28

2 Answers2

25

This is an implementation of Yakk's second option:

template <class... Args>
struct variant_cast_proxy
{
    std::variant<Args...> v;

    template <class... ToArgs>
    operator std::variant<ToArgs...>() const
    {
        return std::visit([](auto&& arg) -> std::variant<ToArgs...> { return arg ; },
                          v);
    }
};

template <class... Args>
auto variant_cast(const std::variant<Args...>& v) -> variant_cast_proxy<Args...>
{
    return {v};
}

You might want to fine tune it for forwarding semantics.

And as you can see its use is simple:

std::variant<int, char> v1 = 24;
std::variant<int, char, bool> v2;

v2 = variant_cast(v1);
bolov
  • 72,283
  • 15
  • 145
  • 224
2

Options:

  • Write your own variant type, possibly inheriting from std::variant, that implements operator= and construction the way you want. Some work has to be done, because variant's constructors can do SFINAE tricks that might not work properly with your variant type; to that end, you want to do some SFINAE forwarding to base-variant yourself instead of naked using declarations.

  • Write a better ConvertVariant that doesn't require the source/destination types to be listed. You would return a convert helper template type that holds the source variant which has an operator std::variant<Ts...>()&& that calls something very much like your ConvertVariant.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524