#include <ContainerUnion.h>
The old good union inherited from C can only swallow primitive data without
constructors and destructors (so called POD types.) There is no analogous construction
for arbitrary user-defined types. One attempt to remedy this has been made in the Boost library
with any_type: it can hold really any data type.
Polymake offers a similar construction, which, however, come closer to the original
union idea in that the set of types assignable to it is strictly defined
at the compile time. In the fact, there are three unions, the first one being the base of the others:
TypeList is a cons list of data types.
Only types listed there can be stored in the union; the checking is performed at compile time.
A type in a list can be also a reference. In this case the union contains a pointer to the data, the latter is expected to be stored elsewhere. You should not specify a data type and a reference to it in the same type list, as these cases can't be distinguished during the assignment.
type_union, iterator_union, or ContainerUnion.
true if it was filled by a constructor or assignment. Return false if
it was unchanged since creation by default constructor.
iterator_union and ContainerUnion are derived from type_union.
They implement the standard iterator and container interface respectively. As the name suggests,
all types in an iterator_union list must be iterators, as well as all types in a ContainerUnion
list must be containers. In the case the listed component classes are of different categories,
the interface implemented by the union corresponds to the most common category.
For example, a union of a forward and random-access iterator will offer an interface of forward iterator.
An intricate issue is the determination of a right data type pointed to by the iterator_union.
The decision is made, based on the typename std::iterator_traits::reference for all component types,
along the following rules:
reference types are identical, the solution is trivial.
reference types are really references and they point to classes related to each other
by inheritance, then the reference of the union is a reference to the least-derived class.
reference are containers, the resulting reference is a ContainerUnion
reference is a type_union. No attempt is made to find a built-in
or user-defined conversion between the data types. If you prefer to have such a conversion instead of creating
type_union objects, you must wrap some of the data containers participating in the union in a
TransformedContainer.
ContainerUnion decides about its iterator type along similar lines:
iterator_union of iterators of the component containers.