Boost logo

Boost :

Subject: Re: [boost] [gsoc] improving any and variant
From: Eric Friedman (ebf_at_[hidden])
Date: 2009-04-06 14:57:10


Mathias,

On Sun, Mar 29, 2009 at 7:01 PM, Mathias Gaunard <
mathias.gaunard_at_[hidden]> wrote:

> - Allow the easy construction of a visitor from lambda expressions, with a
> syntax like
>
> variant<int, std::string> a = "foo";
>
> int b = match(a).with(
> args<int>(_1),
> args<std::string>(size(_1))
> );
>
> instead of the potentially more verbose
>
> struct my_visitor : static_visitor<int>
> {
> int operator()(int a) { return a; }
> int operator()(const std::string& b) { return b.size(); }
> };
>
> variant<int, std::string> a = "foo";;
>
> int b = apply_visitor(my_visitor(), a);

You may find this useful:

https://svn.boost.org/svn/boost/sandbox/boost/type_switch.hpp

http://lists.boost.org/Archives/boost/2003/10/54774.php

I had started on it some years ago, but never followed through. It has some
cool features, though, like pattern matching:

   using namespace boost::type_switch;

   variant<
       int
     , pair<int,double>
     , pair<int,int>
     , pair<double,int>
> var;

   switch_(var)
     |= case_< pair<_1,_1> >(...) // matches pair<int,int>
     |= case_< pair<int,_> >(...) // matches pair<int,*>
     |= case_< pair<_,_> >(...) // matches pair<*,*>
     |= case_< int >(...)
     ;

Eric


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