cxxomfort/optional
Cxxomfort offers an optional implementation similar to that of C++17's std::optional. The cxxomfort implementation has the following interface:
template <typename T> class optional { public: typedef T value_type; optional () noexcept; // disengaged optional (optional const&); // copy optional (nullopt_t) noexcept; // disengaged optional (T const&); // engaged template <typename... Args> optional (std::in_place_t, Args...&&); optional& operator= (optional const&); optional& operator= (nullopt_t) noexcept; // disengages object optional& operator= (T const&); bool has_value () const noexcept; T const& value () const noexcept; T value_or (U) const noexcept; // U convertible to T void swap (optional&); void reset () noexcept; // disengages object T const* get_ptr () const noexcept; // pointer to T, or nullptr explicit operator bool () const noexcept; T const& operator * () const noexcept; friend bool operator== (optional const&, optional const&); friend bool operator!= (optional const&, optional const&); }; // create an optional<T> given a list of arguments to the constructor template <typename T, typename... Args> optional<T> make_optional (Args...);
Where possible, interface elements are compatible with older versions of the Standard (eg.: some noexcept members are declared throw()).
Details specific to cxxomfort::optional and relevant differences from the std implementation:
- Does not implement emplace member functions.
- Implements member get_ptr
- optional<T> for C++'s fundamental types except void are optimized to not require placement new operator nor specialized storage, as the effects of construction and assignment are not observable.
- optional<bool>, optional<cxxomfort::rvoid> are optimized down to 1-byte.
- (pre-C++11) Is only trivially copyable if std::is_fundamental<T>.
- (pre-C++11) If __alignof and memory tools are not supported, implements via external pointer storage instead of via placement new.