C++ Logo


Advanced search

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

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Tue, 8 Sep 2020 09:11:04 -0400
On Tue, Sep 8, 2020 at 6:37 AM Victor Khomenko <
victor.khomenko_at_[hidden]> wrote:

> Hi Arthur,
> What you suggested is one of the workarounds I described in the original
> posting - it works for Motivating Example 1 but not for Motivating Example
> 2.

You gave only one-half of a motivating example. I fleshed it out into a
whole one. I asked you to show the code relating to the vague "AST" idea,
but you haven't done so yet.

> The problems are:
> * If you wish to re-use the visitor, you need a mechanism to update the
> captured reference, and you must remember to update it every time you call
> the visitor, and there can be many call sites.

In the code I showed, the visitor is stateless; it has no captures.

> * If the variant is an rvalue, you end up holding two references (of
> different types) to the same object, so you either forfeit rvalue-ness or
> break the unique ownership semantics.

Can you show an example?


Received on 2020-09-08 08:14:45