Boost logo

Boost Users :

Subject: Re: [Boost-users] variant: bounded types derive from same base class
From: Igor R (boost.lists_at_[hidden])
Date: 2009-08-03 08:56:07


> typedef boost::variant<D1, ... , Dn>  ABCDerived;
>
> and the user could write visitors to visit ABCDerived objects,
> here the user is enforced to implement all cases in the visitor.
>
> but then the function f expects ABC objects. Can I still do:
>
> void f( const ABC& abc )
> {
>   // how to write code to use the visitors with abc ?
> }

struct YourVisitor : boost::static_visitor<functor return type>
{
  result_type operator()(const D1 &d1)
  {...}
  result_type operator()(const D2 &d1)
  {...}
  // etc. for all the type in the variant
};

then:

boost::apply_visitor(YourVisitor(), yourVariant);


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net