cxxo::VocabularyTypes
An extension to cxxomfort.
Implementation of C++17's new "vocabulary types" - optional , variant and any, built around cxxomfort's facilities.
- Type-Erased One-Value Container: cxxomfort/any - an implementation of std::any, which can store a value of any compatible type.
- Nullable Type: cxxomfort/optional - a Nullable type representing the concept of "a value of T or Nothing".
- Discriminated Union: cxxomfort/variant - a typesafe Discriminate Union that represents the concept of "a value of any of these Types".
As with the rest of cxxomfort, these utilities live in namespace cxxomfort::cxxostd, but their headers also lift them to namespace cxxomfort.
A set of C++-like extension-less headers is provided that also lifts the interfaces corresponding to namespace std.
Besides these backports, cxxomfort also adds a few assist utilities not available in the standard:
namespace cxxomfort { T const& get_or (optional<T> opt, T const& alt) ; //< analogue to SQL's ifnull, etc... // returns either the value of 'opt' if it has one, or the alternative 'alt' otherwise. variant<T0, T1, T2, ...> coalesce_optionals (optional<T1> o1, optional<T2> o2, ...) ; //< reunites various optional types into a variant that can hold their types. // returns a variant of (T0 and all the Ts...) // holding the value in the first optional argument found holding a value, or T0 if none. }
Usage
Clone/download and add the vocabulary directory to the compiler's search paths.
Include as:
#include <cxxomfort/base.hpp> // this will head to cxxomfort's includes #include <cxxomfort/any.hpp> #include <cxxomfort/optional.hpp> #include <cxxomfort/variant.hpp> cxxomfort::any myany; cxxomfort::optional<int> maybeint; cxxomfort::variant<string,wstring> somekindofstring;
Or like this, if you are using cxxomfort's transparent headers:
#include <cxxomfort/base.hpp> // this will head to cxxomfort's includes #include <any> #include <optional> #include <variant>
If the compiler is running in C++17 mode or above, the C++-like headers forward to the Standard headers and do nothing on their own except for providing the accesory utilities.
Documentation
For documentation on how to use those library types and associated utilities, check documentation such as cppreference:
For a list of differences or behavious specific to this cxxomfort implementation check cxxomfort/any, cxxomfort/optional, cxxomfort/variant.