C++ Logo

std-discussion

Advanced search

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

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Fri, 21 Aug 2020 17:22:29 +0300
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.

Received on 2020-08-21 09:26:05