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>)'
This is an implementation of Yakk's second option.
To use proper forward semantics the proxy must hold a reference. That reference must be either an lvalue or rvalue reference, preserve consteness and then we need proper forward semantics on that reference. It could be made with a templated proxy, but not in an easy way. In this case it's way simpler to just have 3 proxy classes for the types of references we need (&
, const&
and &&
):
namespace impl
{
template <class... Args>
struct variant_cast_proxy_lref
{
std::variant<Args...>& v;
template <class... ToArgs>
constexpr operator std::variant<ToArgs...>() &&
{
return std::visit(
[](auto&& arg) -> std::variant<ToArgs...>
{
return std::forward<decltype(arg)>(arg);
},
v);
}
};
template <class... Args>
struct variant_cast_proxy_constlref
{
const std::variant<Args...>& v;
template <class... ToArgs>
constexpr operator std::variant<ToArgs...>() &&
{
return std::visit(
[](auto&& arg) -> std::variant<ToArgs...>
{
return std::forward<decltype(arg)>(arg);
},
v);
}
};
template <class... Args>
struct variant_cast_proxy_rref
{
std::variant<Args...>&& v;
template <class... ToArgs>
constexpr operator std::variant<ToArgs...>()&&
{
return std::visit(
[](auto&& arg) -> std::variant<ToArgs...>
{
return std::forward<decltype(arg)>(arg);
},
std::move(v));
}
};
} // namespace impl
Now we need 3 overloads for the variant_cast
function:
template <class... Args>
constexpr impl::variant_cast_proxy_lref<Args...>
variant_cast(std::variant<Args...>& v)
{
return {v};
}
template <class... Args>
constexpr impl::variant_cast_proxy_constlref<Args...>
variant_cast(const std::variant<Args...>& v)
{
return {v};
}
template <class... Args>
constexpr impl::variant_cast_proxy_rref<Args...>
variant_cast(std::variant<Args...>&& v)
{
return {std::move(v)};
}
And as you can see its use is simple:
std::variant<int, char> v1 = 24;
const std::variant<int, char> cv1 = 24;
std::variant<int, char, bool> v2;
v2 = variant_cast(v1);
v2 = variant_cast(cv1);
v2 = variant_cast(std::move(v1));
template <class... Args>
struct variant_cast_proxy
{
std::variant<Args...> v;
template <class... ToArgs>
constexpr operator std::variant<ToArgs...>() &&
{
return std::visit(
[](auto& arg) -> std::variant<ToArgs...>
{
return std::move(arg);
},
std::move(v));
}
};
template <class... Args>
constexpr auto variant_cast(std::variant<Args...> v)
-> variant_cast_proxy<Args...>
{
return {std::move(v)};
}