On Mon, Sep 7, 2020 at 12:50 PM Victor Khomenko via Std-Proposals <std-proposals@lists.isocpp.org> wrote:

Problem: How to change the value of a variant from within a visitor? Unfortunately, the relevant operator()(Alternative&) of the visitor has no access to the enclosing std::variant. Not only this problem is annoying, but it's also messy to work around, as explained below. [...]

Motivating Example 1: Visitor turning a Caterpillar to a Butterfly:

        using MyVariant = variant<Caterpillar,Butterfly>;

        struct Visitor{
                void operator()(Caterpillar& c){ variant_from_alternative<MyVariant>(c) = Butterfly(); }
                ...
        };

Could you show both sides of this example — both callee and caller?  I would expect the caller to look like this:

    MyVariant foo;
    Visitor visitor;
    std::visit(visitor, foo);

which means it'd be trivial to change it to look like this instead:

    struct Visitor {
        void operator()(Caterpillar&, MyVariant& v) const { v = Butterfly(); }
    };

    MyVariant foo;
    Visitor visitor;
    std::visit([&](auto& c) {
        visitor(c, foo);
    }, foo);

Can you show a motivating example (both sides — caller and callee) where the solution isn't this simple?

HTH.
Arthur