vendredi 27 octobre 2023

Assigning std::variant from sub-variant

Given a std::variant<A,B,C,...> is it possible to assign from a aub-type std::variant<A, B>?

On MSVC 2019 I get "no suitable user defined conversion" on direct assignment. With std::visit one could implement the folowing:

std::variant<A, B, C> x;
std::variant<A, B> y;
std::visit([&x](const auto& value) {
    x = value;
}, y);

Is there a more elegant way to implement such an assignment?

Aucun commentaire:

Enregistrer un commentaire