Boost logo

Boost :

From: Yitzhak Sapir (yitzhaks_at_[hidden])
Date: 2002-06-03 10:14:48


> I'm confused. What class should define the virtual function;
> and how does
> a virtual function accept an unknown type? Templates and
> virtuals don't mix
> well; templates define an potentially unbounded set of
> functions and vtables
> contain a fixed number of functions.

The class that defines the virtual function is placeholder (that defines in the current boost::any the virtual destructor, clone() and type() functions).
The class that implements the virtual function is holder, which inherits from placeholder. The implementation simply applies the functor type to "held" in the current boost::any.
The type of the functor is templated, but it is a template parameter of "any", so that for a given instantiation of "any" the type is known and the definitions of placeholder and holder<T> are complete. Templates and virtual functions won't work well only if the virtual function is expected to be templated. This isn't what I was suggesting.

>
> You could still implement visitior using the techniques from
> MC++D, though,
> it just works differently ( with typelist, as you mentioned).

This technique would work for one functor, but using typelists, it could probably be extended to n functors.


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