cxxomfort/optional

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: