C++ Logo

std-proposals

Advanced search

Re: std::variant - going from Alternative& to the enclosing variant&

From: Victor Khomenko <victor.khomenko_at_[hidden]>
Date: Fri, 11 Sep 2020 15:22:15 +0000
> > void operator(my_variant &v, std::variant_tag<1> t) {
> > auto& w = std::get<t.index>(v);
> > }
>
> ```
> void operator(my_variant &v, std::variant_tag<1> t) {
> auto& w = t(v); // same as `std::get<t.index>(v);`
> }
> ```

Nice ideas! They still require more typing, namely an extra parameter and an extra line of code to recover the alternative. How about going a step further, and capture the variant inside the tag, and make the tag convertible to Alternative&, e.g.:

void operator(std::variant_tag<1> t) {
        do_something_with_variant(t.get_variant());
        do_something_with_alternative(t); // t is converted to Alternative& or const Alternative&
        do_something_else_with_alternative(move(t)); // converted to Alternative&&
}

Received on 2020-09-11 10:25:49