[optional.optional.general] (original) (raw)

22 General utilities library [utilities]

22.5 Optional objects [optional]

22.5.3 Class template optional [optional.optional]

22.5.3.1 General [optional.optional.general]

namespace std { template<class T> class optional { public: using value_type = T;using iterator = implementation-defined; using const_iterator = implementation-defined; constexpr optional() noexcept;constexpr optional(nullopt_t) noexcept;constexpr optional(const optional&);constexpr optional(optional&&) noexcept(see below);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 = remove_cv_t<T>> constexpr explicit(see below) optional(U&&);template<class U> constexpr explicit(see below) optional(const optional<U>&);template<class U> constexpr explicit(see below) optional(optional<U>&&);constexpr ~optional();constexpr optional& operator=(nullopt_t) noexcept;constexpr optional& operator=(const optional&);constexpr optional& operator=(optional&&) noexcept(see below);template<class U = remove_cv_t<T>> constexpr optional& operator=(U&&);template<class U> constexpr optional& operator=(const optional<U>&);template<class U> constexpr optional& operator=(optional<U>&&);template<class... Args> constexpr T& emplace(Args&&...);template<class U, class... Args> constexpr T& emplace(initializer_list<U>, Args&&...);constexpr void swap(optional&) noexcept(see below);constexpr iterator begin() noexcept;constexpr const_iterator begin() const noexcept;constexpr iterator end() noexcept;constexpr const_iterator end() const noexcept;constexpr const T* operator->() const noexcept;constexpr T* operator->() noexcept;constexpr const T& operator*() const & noexcept;constexpr T& operator*() & noexcept;constexpr T&& operator*() && noexcept;constexpr const T&& operator*() const && noexcept;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 = remove_cv_t<T>> constexpr T value_or(U&&) const &;template<class U = remove_cv_t<T>> constexpr T value_or(U&&) &&;template<class F> constexpr auto and_then(F&& f) &;template<class F> constexpr auto and_then(F&& f) &&;template<class F> constexpr auto and_then(F&& f) const &;template<class F> constexpr auto and_then(F&& f) const &&;template<class F> constexpr auto transform(F&& f) &;template<class F> constexpr auto transform(F&& f) &&;template<class F> constexpr auto transform(F&& f) const &;template<class F> constexpr auto transform(F&& f) const &&;template<class F> constexpr optional or_else(F&& f) &&;template<class F> constexpr optional or_else(F&& f) const &;constexpr void reset() noexcept;private: union { remove_cv_t<T> val; };};template<class T> optional(T) -> optional<T>;}

An instance of optional<T> is said tocontain a valuewhen and only when its member val is active ([class.union.general]);val is referred to as itscontained value.

An optional object's contained value is nested within ([intro.object]) the optional object.

A type X is avalid contained type for optionalif X is an lvalue reference type or a complete non-array object type, and remove_cvref_t<X> is a type other than in_place_t or nullopt_t.

If a specialization of optional is instantiated with a type Tthat is not a valid contained type for optional, the program is ill-formed.

If T is an object type,T shall meet the Cpp17Destructible requirements (Table 35).