Standard library header <optional> - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# Standard library header <optional>

From cppreference.com
< cpp‎ | header

### [edit] Classes

Name | Description
optional (since C++17) | a wrapper that may or may not hold an object (class template) [edit]
bad_optional_access (C++17) | exception indicating checked access to an optional that doesn't contain a value (class) [edit]
std::hash<std::optional> (C++17) | specializes the std::hash algorithm (class template specialization) [edit]
nullopt_t (C++17) | indicator of optional type with uninitialized state (class) [edit]

### [edit] Functions

##### Comparison
operator==operator!=operator<operator<=operator>operator>= (C++17) | compares `optional` objects (function template) [edit]
##### Specialized algorithms
std::swap(std::optional) (C++17) | specializes the std::swap algorithm (function) [edit]
make_optional (C++17) | creates an `optional` object (function template) [edit]

### [edit] Synopsis

    namespace std {
      // optional
      template <class T> class optional;
      // no-value state indicator
      struct nullopt_t{/*see definition*/ };
      constexpr nullopt_t nullopt(/*unspecified*/);
      // class bad_optional_access
      class bad_optional_access;
      // 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>&);
      template <class T>
      constexpr bool operator<(const optional<T>&, const optional<T>&);
      template <class T>
      constexpr bool operator>(const optional<T>&, const optional<T>&);
      template <class T>
      constexpr bool operator<=(const optional<T>&, const optional<T>&);
      template <class T>
      constexpr bool operator>=(const optional<T>&, const optional<T>&);
      // 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;
      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;
      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;
      // 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>&);
      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>&);
      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>&);
      // specialized algorithms
      template <class T> void swap(optional<T>&, optional<T>&) noexcept(/*see definition*/);
      template <class T> constexpr optional</*see definition*/> make_optional(T&&);
      template <class T, class... Args>
      constexpr optional<T> make_optional(Args&&... args);
      template <class T, class U, class... Args>
      constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args);
      // hash support
      template <class T> struct hash;
      template <class T> struct hash<optional<T>>;
    }

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

    template <class T> class optional {
    public:
      using value_type = T;
      // constructors
      constexpr optional() noexcept;
      constexpr optional(nullopt_t) noexcept;
      optional(const optional&);
      optional(optional&&) noexcept(/*see definition*/ );
      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 &&...);
      template <class U = T> /*conditionally-explicit*/ constexpr optional(U&&);
      template <class U> /*conditionally-explicit*/ optional(const optional<U>&);
      template <class U> /*conditionally-explicit*/ optional(optional<U> &&);
      // destructor
      ~optional();
      // assignment
      optional& operator=(nullopt_t) noexcept;
      optional& operator=(const optional&);
      optional& operator=(optional&&) noexcept(see below );
      template <class U = T> optional& operator=(U&&);
      template <class U> optional& operator=(const optional<U>&);
      template <class U> optional& operator=(optional<U>&&);
      template <class... Args> void emplace(Args&&...);
      template <class U, class... Args>
      void emplace(initializer_list<U>, Args&&...);
      // swap
      void swap(optional&) noexcept(see below );
      // observers
      constexpr const T* operator->() const;
      constexpr T* operator->();
      constexpr const T& operator*() const&;
      constexpr T& operator*() &;
      constexpr T&& operator*() &&;
      constexpr const T&& operator*() const&&;
      constexpr explicit operator bool() const noexcept;
      constexpr bool has_value() const noexcept;
      constexpr const T& value() const&;
      constexpr T& value() &;
      constexpr T&& value() &&;
      constexpr const T&& value() const&&;
      template <class U> constexpr T value_or(U&&) const&;
      template <class U> constexpr T value_or(U&&) &&;
      // modifiers
      void reset() noexcept;
    private:
      T *val; // exposition only
    };

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