Standard library header <optional> (C++17)
From cppreference.net
Этот заголовок является частью библиотеки общих утилит .
Включения |
||
|
(C++20)
|
Поддержка оператора трёхстороннего сравнения | |
Классы |
||
|
(C++17)
|
обёртка, которая может содержать или не содержать объект
(шаблон класса) |
|
|
(C++17)
|
исключение, указывающее на проверенный доступ к optional, не содержащему значения
(класс) |
|
|
(C++17)
|
поддержка хеширования для
std::optional
(специализация шаблона класса) |
|
|
(C++17)
|
индикатор
std::optional
, который не содержит значения
(класс) |
|
Предварительные объявления |
||
|
Определено в заголовке
<functional>
|
||
|
(C++11)
|
функциональный объект хеширования
(шаблон класса) |
|
Константы |
||
|
(C++17)
|
объект типа
nullopt_t
(константа) |
|
Функции |
||
Сравнение |
||
|
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++20)
|
сравнивает объекты
optional
(шаблон функции) |
|
Специализированные алгоритмы |
||
|
(C++17)
|
специализирует алгоритм
std::swap
(шаблон функции) |
|
|
(C++17)
|
создаёт объект
optional
(шаблон функции) |
|
Синопсис
// преимущественно независимая #include <compare> namespace std { // шаблон класса optional template<class T> class optional; // частично независимый template<class T> constexpr bool ranges::enable_view<optional<T>> = true; template<class T> constexpr auto format_kind<optional<T>> = range_format::отключено; template<class T> concept /*is-derived-from-optional*/ = requires(const T& t) { // exposition-only []<class U>(const optional<U>&) {}(t); }; // индикатор состояния без значения struct nullopt_t { /* см. описание */ }; inline constexpr nullopt_t nullopt(/* не указано */); // class bad_optional_access class bad_optional_access; // операторы отношений template<class T, class U> constexpr bool operator==(const optional<T>&, const optional<U>&); template<class T, class U> constexpr bool operator!=(const optional<T>&, const optional<U>&); template<class T, class U> constexpr bool operator<(const optional<T>&, const optional<U>&); template<class T, class U> constexpr bool operator>(const optional<T>&, const optional<U>&); template<class T, class U> constexpr bool operator<=(const optional<T>&, const optional<U>&); template<class T, class U> constexpr bool operator>=(const optional<T>&, const optional<U>&); template<class T, three_way_comparable_with<T> U> constexpr compare_three_way_result_t<T, U> operator<=>(const optional<T>&, const optional<U>&); // сравнение с nullopt template<class T> constexpr bool operator==(const optional<T>&, nullopt_t) noexcept; template<class T> constexpr strong_ordering operator<=>(const optional<T>&, nullopt_t) noexcept; // сравнение с T template<class T, class U> constexpr bool operator==(const optional<T>&, const U&); template<class T, class U> constexpr bool operator==(const T&, const optional<U>&); template<class T, class U> constexpr bool operator!=(const optional<T>&, const U&); template<class T, class U> constexpr bool operator!=(const T&, const optional<U>&); template<class T, class U> constexpr bool operator<(const optional<T>&, const U&); template<class T, class U> constexpr bool operator<(const T&, const optional<U>&); template<class T, class U> constexpr bool operator>(const optional<T>&, const U&); template<class T, class U> constexpr bool operator>(const T&, const optional<U>&); template<class T, class U> constexpr bool operator<=(const optional<T>&, const U&); template<class T, class U> constexpr bool operator<=(const T&, const optional<U>&); template<class T, class U> constexpr bool operator>=(const optional<T>&, const U&); template<class T, class U> constexpr bool operator>=(const T&, const optional<U>&); template<class T, class U> requires(!/*is-derived-from-optional*/<U>) && three_way_comparable_with<T, U> constexpr compare_three_way_result_t<T, U> operator<=>(const optional<T>&, const U&); // специализированные алгоритмы template<class T> constexpr void swap(optional<T>&, optional<T>&) noexcept(/* см. описание */); template<class T> constexpr optional<decay_t<T>> 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); // поддержка хеширования template<class T> struct hash; template<class T> struct hash<optional<T>>; }
Шаблон класса std::optional
namespace std { template<class T> class optional { public: using value_type = T; using iterator = /* определяемое реализацией */; using const_iterator = /* определяемый реализацией */; // конструкторы constexpr optional() noexcept; constexpr optional(nullopt_t) noexcept; constexpr optional(const optional&); constexpr optional(optional&&) noexcept(/* см. описание */); 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(/* см. описание */) optional(U&&); template<class U> constexpr explicit(/* см. описание */) optional(const optional<U>&); template<class U> constexpr explicit(/* см. описание */) optional(optional<U>&&); // деструктор constexpr ~optional(); // присваивание constexpr optional& operator=(nullopt_t) noexcept; constexpr optional& operator=(const optional&); constexpr optional& operator=(optional&&) noexcept(/* см. описание */); 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&&...); // swap constexpr void swap(optional&) noexcept(/* см. описание */); // поддержка итераторов 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&; // freestanding-deleted constexpr T& value() &; // freestanding-deleted constexpr T&& value() &&; // freestanding-deleted constexpr const T&& value() const&&; // freestanding-deleted 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: T* val; // exposition-only }; template<class T> optional(T) -> optional<T>; }
Шаблон класса std::bad_optional_access
namespace std { class bad_optional_access : public exception { public: // для спецификации специальных функций-членов constexpr const char* what() const noexcept override; }; }