Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-06-13 09:40:20


From: "Itay Maman" <itay_maman_at_[hidden]>
> Peter Dimov wrote:
> > From: "Itay Maman" <itay_maman_at_[hidden]>
> >
> >>The variant implementation relies on the ability to map concrete types
> >>to integers. My implementation uses the index_of TypeList algorithm, for
> >>instance.
> >>
> >>So, unless we'll come-up with some birlliant way to do this mapping
> >>differently, we will not be able to implement a variant whose list of
> >>legal (concrete) types is 'open'.
> >
> >
> > It is possible to implement an 'open' list via inheritance.
> >
>
> Sure, but this is intrusive (which is unacceptable for a variant class),
> isn't it?

No, what I had in mind isn't intrusive. My 'variant<T>' thing that's been
lying in the files area for ages is a variant where the set of the possible
types is bounded by inheritance, i.e. it can store a U is U* is convertible
to T*. boost::any is pretty much equivalent to variant<void>.

This variant is not a discriminated union, though. You can't enumerate the
alternatives in a switch. You could, however, reuse the idea to implement a
recursive discriminated union. Such a variant<L> would store either values
of type T where T belongs to L, or values of type U that inherits from
variant_base, the base of all variant<L>'s.

To implement a typesafe cast, variant_base would have a virtual void *
cast_to(type_info const &) method.


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