Boost logo

Boost :

From: Eric Friedman (ebf_at_[hidden])
Date: 2007-09-08 14:22:43


Christian,

On 9/7/07, Christian Holmquist <c.holmquist_at_[hidden]> wrote:
> Is there any visitor mechanism somewhere in boost that allows one to
> introspect the contained type of a boost::any?
> I've been using boost::variant frequently and I love the way its visitor
> works, but it's not always practical to know all possible types at compile
> time (library dependencies, compilation time etc..).
[snip]
> Does anyone else have a use for this, or have another solution at hand?

Some long time back I had added support into Boost.Any for visitation:

http://boost-sandbox.cvs.sourceforge.net/boost-sandbox/boost-sandbox/boost/any.hpp?view=markup

http://boost-sandbox.cvs.sourceforge.net/boost-sandbox/boost-sandbox/libs/any/any_test.cpp?view=markup

Basically, the interface is as seen in the `test_dynamic_visit` test case:

  class my_visitor
    : public boost::dynamic_visitor_base,
      public boost::dynamic_visitor_interface<T1>,
      ...,
      public boost::dynamic_visitor_interface<TN>
  {
    ...

  public:

     /*override*/ void visit(T1&);
     ...
     /*override*/ void visit(TN&);

  };

and then an instance of the visitor may be passed to a boost::any:

  boost::any a = ...;
  my_visitor v;
  boost::apply_visitor(v, a);

Of course this assumes (parts of) the Boost.Visitor library, also in
the sandbox.

In any case, something based on this idea might be another alternative
to what you're proposing.

Eric


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