C++ Logo

STD-DISCUSSION

Advanced search

Subject: Re: std::variant - going from Alternative& to the enclosing variant&
From: Ville Voutilainen (ville.voutilainen_at_[hidden])
Date: 2020-08-21 09:22:29


On Fri, 21 Aug 2020 at 17:17, Victor Khomenko
<victor.khomenko_at_[hidden]> wrote:
>
> Thanks Ville,
>
> >Use a lambda as a visitor and capture the variant in the lambda?
>
> This won't work if the visitor requires very different processing for different Alternatives. My proposed workaround with a pointer in a visitor is similar to what you suggest, but your solution is restricted to the case when a single lambda can handle every alternative.

A single lambda can handle every alternative. You can do an
if-constexpr in a polymorphic lambda, and handle different
cases in any way you please.

If you want different functions for different alternatives, write a
function object that provides access to the variant.

I suppose we could add a new visit-function that provides the variant
itself as a parameter to the visitor. I'm not entirely
sure whether that's necessary, because the work-arounds are not that
complicated to write.


STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups