Boost logo

Boost :

From: Vesa Karvonen (vesa_karvonen_at_[hidden])
Date: 2002-03-26 04:26:23


There have been many discussions about variant types / discriminated unions
on Boost. At the moment I'm working on something where I could use an
efficient, recursive variant type. Before I consider implementing a variant
type myself, I'd like to know what is the status of current variant type
implementations?

Type safety is the whole point. I can statically specify all the types that
the variant needs to be able to store. This means that Boost.Any has more
flexibility than I need.

Efficiency is important. I simply can not afford unnecessary memory
allocations. (This basically rules out Boost.Any.)

I also need to be able to build recursive variant types. Consider this Ocaml
type definition:

  type value =
    | Int of int
    | Float of float
    | String of string
    | Aggr of value array
                ^^^^^
Most of the variant type proposals I have seen do not support this.

I came up with the following syntax:

  struct value : variant<value // B&N (probably needed)
    , int
    , float
    , string
    , aggr<value>
>
  {
    // ...
  };

(This kind of recursion does not work if the data structure (in this case
aggr<>) wants to instantiate the type.)

This would, of course, require some constructors, etc... to be written by
the user (those could be macroized). Another technique would be to use some
kind of placeholders, but those seem to require special support for each
data structure (in this case aggr<>), which is something I do not like.

  typedef variant<int, float, string, aggr< variant_self > > value;

Does someone have an idea on how to support recursive variant types?

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.


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