Boost logo

Boost :

From: Mac Murrett (mmurrett_at_[hidden])
Date: 2002-07-03 12:55:23

Itay, thanks for responding!

>> I have been looking at the implementations of variant classes
>> floating by here, and while I have been happy with aspects of them,
>> I am not happy with the visitability of any of them.
> Could you elaborate on that?

It seems that we disagree about what is desirable in a visitor
base, but I will try. The principal disadvantage of the other
implementations that I have seen is that they lack a
virtually-dispatched base class. This means that they cannot be
passed down an inheritance hierarchy, so we have to have a hack for
our visitation implementation. With a base class for our visitor,
we can have a virtual accept method on an any-like placeholder and

> The main disadvantage of this implementation - when compared to
> the visitor
> in the suggested variants implementations -
> is that it is too strict. You must supply an appropriate
> visit_impl member
> function for each type on the type-list. This increases
> code duplication, since its very likely that you can handle
> several types by
> one function. To clarify this point, here is a concrete visitor
> which, I think, cannot work with your suggested boost::visitor:
> struct inc_by_10_visitor : visitor<int, short, unsigned int, char>
> {
> void visit_impl(int x)
> {
> cout << "result = " << x+10 << endl;
> }
> };

As stated above, I think that the use of virtuals is a key benefit,
but I agree that being able to trivially implement something like
your example would be nice. Happily, there is a trivial TMP-based
solution to this. This is still not perfect, though, so we'll go
the extra mile. Your example relies on the ability to cast
everything to a "mother type", in this case, int. What would be
far superior would be:

struct inc_by_10_visitor2
        template<typename T>
     void visit_func(T &x)
         cout << "result = " << x + 10 << endl;

We can support both of these by having an adapter template that
creates a visitor from a type list and anything that provides
visit_func suitable for all types in the list.

I have uploaded it to the files section at, but the gist
is that it derives from both visitor<list> and a user-specified
class, and implements the visit_impl functions to call a function
in the user-specified class. See the example named
inc_by_10_visitor for an example.

Thanks again for the feedback,
Mac Murrett.

Boost list run by bdawes at, gregod at, cpdaniel at, john at