##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# Standard library header <optional>

From cppreference.com
< cpp‎ | header

| After reviewing national body comments to n3690, this library component was voted out from C++14 working paper into a separate Technical Specification. It is not a part of the draft C++14 as of n3797.

This header is part of the general utility library.

### Includes
`<initializer_list>`
### Classes
optional (C++14) | a wrapper that may or may not hold an object (class template) [edit]
bad_optional_access (C++14) | exception indicating access to a optional type with uninitialized state (class) [edit]
in_place_t (C++14) | disambiguation tag type for in-place construction of optional types (class) [edit]
std::hash<std::experimental::optional> | specializes the std::hash algorithm (class template specialization) [edit]
nullopt_t (C++14) | indicator of optional type with uninitialized state (class) [edit]
### Functions
operator==operator< | compares `optional` objects (function template) [edit]
make_optional | creates an `optional` object (function template) [edit]
std::swap(std::experimental::optional) | specializes the std::swap algorithm (function) [edit]

### [edit] Synopsis

    #include <initializer_list>
    namespace std {
        // 20.6.4, optional for object types
        template <class T> class optional;
        // 20.6.5, In-place construction
        struct in_place_t{};
        constexpr in_place_t in_place{};
        // 20.6.6, Disengaged state indicator
        struct nullopt_t;
        constexpr nullopt_t nullopt( /*unspecified*/ );
        // 20.6.7, class bad_optional_access
        class bad_optional_access;
        // 20.6.8, Relational operators
        template <class T> constexpr bool operator==(const optional<T>&, const optional<T>&);
        template <class T> constexpr bool operator<(const optional<T>&, const optional<T>&);
        // 20.6.9, Comparison with nullopt
        template <class T> constexpr bool
        operator==(const optional<T>&, nullopt_t) noexcept;
        template <class T> constexpr bool
        operator==(nullopt_t, const optional<T>&) noexcept;
        template <class T> constexpr bool
        operator<(const optional<T>&, nullopt_t) noexcept;
        template <class T> constexpr bool
        operator<(nullopt_t, const optional<T>&) noexcept;
        // 20.6.10, Comparison with T
        template <class T> constexpr bool operator==(const optional<T>&, const T&);
        template <class T> constexpr bool operator==(const T&, const optional<T>&);
        template <class T> constexpr bool operator<(const optional<T>&, const T&);
        template <class T> constexpr bool operator<(const T&, const optional<T>&);
        // 20.6.11, Specialized algorithms
        template <class T> void swap(optional<T>&, optional<T>&) noexcept(see below );
        template <class T> constexpr optional<T> make_optional(T&&);
        // 20.6.12, hash support
        template <class T> struct hash;
        template <class T> struct hash<optional<T>>;
    } // namespace std

#### [edit] Class std::experimental::optional

    template <class T>
    class optional
    {
    public:
        typedef T value_type;

        // 20.6.4.1, constructors
        constexpr optional() noexcept;
        constexpr optional(nullopt_t) noexcept;
        optional(const optional&);
        optional(optional&&) noexcept(see below );
        constexpr optional(const T&);
        constexpr optional(T&&);
        template <class... Args> constexpr explicit optional(in_place_t, Args&&...);
        template <class U, class... Args>
        constexpr explicit optional(in_place_t, initializer_list<U>, Args&&...);

        // 20.6.4.2, destructor
        ~optional();

        // 20.6.4.3, assignment
        optional& operator=(nullopt_t) noexcept;
        optional& operator=(const optional&);
        optional& operator=(optional&&) noexcept(see below );
        template <class U> optional& operator=(U&&);
        template <class... Args> void emplace(Args&&...);
        template <class U, class... Args> void emplace(initializer_list<U>, Args&&...);

        // 20.6.4.4, swap
        void swap(optional&) noexcept(see below );

        // 20.6.4.5, observers
        constexpr T const* operator->() const;
        T* operator->();
        constexpr T const& operator*() const;
        T& operator*();
        constexpr explicit operator bool() const noexcept;
        constexpr T const& value() const;
        T& value();
        template <class U> constexpr T value_or(U&&) const&;
        template <class U> T value_or(U&&) &&;

    private:
        bool init; // exposition only
        T* val; // exposition only
    };

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/header/optional&oldid=68175"