How Does the Variant Library Improve Your Programs?


  • Typesafe storage and retrieval of a user-specified set of types

  • A means to store heterogeneous types in Standard Library containers

  • Compile-time checked visitation of variants

  • Efficient, stack-based storage for variants

The Variant library focuses on typesafe storage and retrieval of a bounded set of typesthat is, on discriminated unions. The Boost.Variant library has many features in common with Boost.Any, but there are different tradeoffs as well as differences in functionality. The need for discriminated unions (variant types) is very common in everyday programming. One typical solution while retaining type safety is to use abstract base classes, but that's not always possible; even when it is, the cost of heap allocation and virtual functions[1] may be too high. One might also try using unsafe indiscriminate types such as void* (which leads to disaster), or typesafe but unbounded variant types, such as Boost.Any. The library we look at hereBoost.Variantsupports bounded variant typesthat is, variants where the elements come from a set of supported types.

[1] Although virtual functions do come with a very reasonable price with regard to performance.

Variant types are available in many other programming languages, and they have proven their worth time and again. There is very limited built-in support in C++ for variant types, only in the form of unions, that exist mainly for C compatibility. Boost.Variant remedies the situation through a class template variant, and accompanying tools for safely storing and retrieving values. A variant data type exposes an interface independent of the current value's type. If you've used some proprietary variant types before, you may have been exposed to types that only support a fixed set of types. That is not the case with this library; you define the set of types that are allowed in a variant when you use it, and a program can contain any number of disparate variant instantiations. To retrieve the value that is held in a variant, you either need to know the exact type of the current value, or use the provided typesafe visitor mechanism. The visitor mechanism makes Variant quite different from most other variant libraries, including Boost.Any (which on the other hand can hold a value of any conceivable type), and thereby enables a safe and robust environment for handling such types. C++ unions are only useful for built-in types and POD types, but this library offers discriminated union support for all types. Finally, efficiency aspects are covered, too, as the library stores its values in stack-based storage, thus avoiding more expensive heap allocations.



    Beyond the C++ Standard Library(c) An Introduction to Boost
    Beyond the C++ Standard Library: An Introduction to Boost
    ISBN: 0321133544
    EAN: 2147483647
    Year: 2006
    Pages: 125

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net