BOOST WIKI | RecentChanges | Preferences | Page List | Links List
A Variant library would provide a class template that acts as a discriminated union. A discriminated union is like a C-style union in that it stores one value whose type may be one of several types listed when building the union. However, a discriminated union tracks which type of value it currently holds, and is therefore type safe (C-style unions are not type safe).
A Variant library should provide a variant class template with the following properties:
- Interface should support construction and assignment from any of the types that the variant can hold or any type convertible to one of the types that the variant can hold.
- Interface should be similar to that of boost::any for usability reasons.
- Support for all types, e.g., types with nontrivial constructors, assignment operators, or destructors.
- Stack allocation should be used/allowed so that the variant can be stored efficiently. Stack allocation should be easy using the type_with_alignment type trait and the compile-time least common denominator routines. The upcoming Metaprogramming Library may help as well.
- Recursive types should be allowed, i.e., a variant can hold a value of a type that is constructed using the variant type. Heap allocation is required in such cases. Recursive types were discussed here.
- Incomplete types should be allowed.
- Provide constructs for performing operations based on the type of the value stored in the variant that can check completeness of decoding. For instance, a 'typeswitch' or visitor construct that is guaranteed to fail at compile time if not all cases are covered. Pattern-matching constructs would be extremely interesting. Syntactical possibilities were discussed here.
Here are some reference links to Andrei's writings on this subject:
and older:
Disclaimer: This site not officially maintained by Boost Developers