Boost logo

Boost :

Subject: Re: [boost] [variant] match()
From: pfultz2 (pfultz2_at_[hidden])
Date: 2015-01-07 16:21:52


> My personal stance and I what I use on projects is something a little
> different and I try to evangelize: Make an overloads template that just
> works with apply_visitor or an apply_visitor like function:
>
>
> apply_visitor( overloads( [](A & a){ cout << "it's a A!"; }
> [](B & b){ cout << "oh, a B!"; }
> [](C & c){ cout << "ah yes, a C!"; }
> [](auto & d){ cout << "a default!"; }
> ),
>
> v
>
> );

I use syntax like this:

case_<void>(v)(
    [](A & a){ cout << "it's a A!"; }
    [](B & b){ cout << "oh, a B!"; }
    [](C & c){ cout << "ah yes, a C!"; }
    [](auto & d){ cout << "a default!"; }
);

Which I think is a little neater. You can implement this in C++14 with Fit
like this:

template<class T, class Result>
Result case_(T& x)
{
    return [&](auto&&... fs)
    {
        return apply_visitor(x,
           
fit::result<Result>(fit::match(std::forward<decltype(fs)>(fs)...))
        );
    };
}

This could also be extended to support multiple variables as well.

Paul

--
View this message in context: http://boost.2283326.n4.nabble.com/variant-match-tp4670714p4670758.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

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