Boost logo

Boost :

From: Mac Murrett (mmurrett_at_[hidden])
Date: 2002-07-09 13:47:06


Itay,

Thank you for your continued feedback! I will try to clear a few
things up for you.

> Templates and virtual functions do not mix. A virtual base class
> creates a
> 'firewall' which, has the nice effect of allowing polymorphic
> objects to
> pass thru, but, on the other hands, it hides any concrete type
> information.
> This, obviously, reduces the knowledge the compiler has about the
> passed-in
> visitor, and decreases flexibility.

We're all professional developers and C++ experts here, and I feel
that this rather pedantic and over-general statement has no bearing
on this discussion.

Furthermore, I believe that the method used in the visitor that I
proposed is a full-blown virtual template function idiom. It
allows us to specify a list of types and a template definition, and
get virtual functions for each of the types in the type list.

> As for the inc_by_10_visitor, I do agree with you that a template
> member
> function offers even more power, I just wanted to give a quick
> example to
> clarify the point. [I was using the well known "clarify-by-code" idiom
> :)....].

That's OK, we've solved both the first and revised problem.

> Anyway, I did look at the example and I saw that you did not try
> to visit
> boost::any objects. We still need a virtual base class which can
> be passed
> thru boost::any::placeholder. And I just don't see how your code offers
> that. (Again, Il'l be happy to stand corrected). In other words:
> If you only
> need to visit simple classes (non any-like data types) there's no
> need for
> all this inheritance.

I thought that the purpose of a variant class was to overcome the
shortcomings of boost::any. A variant whose type is restricted to
those in a type list would benefit from this visitor. A variant
that can hold any type, such as boost::any, would not. This is
because by the time something is in a boost::any, the compiler has
lost track of what the real type is. Using type lists, we can
create a variant that can represent an instance of any type in the
type list, and have the compiler dispatch visitation (among other
things).

If we know that an instance of our variant is of some type in a
known list, why not exploit that in our visitor? If we can
automatically generate a pure-virtual function for each type in the
list of types, why not use that for a visitor base class? If we
can then use a convenience template to generate implementations for
those virtual functions to call a template function, why not use
that to generate our visitors?

In short, I do not see an advantage to not exploiting what the
compiler already knows about the type, unless all that we are
trying to do is recreate boost::any.

Mac Murrett.


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