Boost logo

Boost :

Subject: Re: [boost] Variant and visitation (was: [optional] Safe optional)
From: Matt Calabrese (rivorus_at_[hidden])
Date: 2014-11-18 18:06:43


On Tue, Nov 18, 2014 at 2:52 PM, Sebastian Redl <
sebastian.redl_at_[hidden]> wrote:

>
> On 18 Nov 2014, at 22:59, Antony Polukhin <antoshkka_at_[hidden]> wrote:
>
> > 2014-11-19 0:40 GMT+03:00 Nevin Liber <nevin_at_[hidden]>:
> >>
> >> It's the inversion of control that people just don't like.
> >>
> >
> > As a guy, who's been keeping an eye on Boost.Variant for last two years,
> I
> > was planning to add support for generalized lambdas as a visitors for
> > variant:
> >
> > apply_visitor(
> > [](auto v){ std::cout << v; },
> > variant_variable
> > );
>
> Alternatively, it could be done like this:
>
> apply_visitor(variant_variable,
> [](int i) { std::cout << “int: “ << i; },
> [](float f) { std::cout << “float: “ << f; },
> otherwise([](auto v) { std::cout << “something: “ << v; });
>
> I’ve recently written something very similar for boost::any, using runtime
> checks instead of course.

The problem with that is it doesn't generalize easily to n-ary visitation.

I have a separate variant and visitation implementation that I've been
planning to propose for standardization, but I don't know if it will happen
especially since others are on their way. My approach is:

// Called dispatch because of negative connotations of visitor
dispatch( overloads( []( int,some_type, float ) {}, []( auto, auto, auto {}
), a_variant, pass_through( not_a_variant ), another_variant );

All "overloads" does is form an overload set via base-class chaining and
bringing in operator() with "using" (appropriately wraps function pointers
so they work as well). Return types are deduced by a slightly modified
common_type mechanism.

-- 
-Matt Calabrese

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk