optional

标准库标头<optional>

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

功能

比较

*。

运算符==运算符%21=运算符<操作符<=运算符>操作符>=%28C++17%29比较可选对象%28函数模板%29

专用算法

调换%28std::可选的%29%28C++17%29专门使用std::swp算法%28函数%29

制造[医]可选%28C++17%29创建可选对象%28函数模板%29

简介

二次

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>>; }

二次

类模板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 };

二次

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/报头/可选