Заголовочный файл стандартной библиотеки <optional> (C++17)
Материал из cppreference.com
Включает | |
(C++20) |
Поддержка оператора трёхстороннего сравнения |
(C++17) |
обёртка, которая может содержать или не содержать объект (шаблон класса) |
(C++17) |
исключение, указывающее на доступ на проверку к optional, не содержащему значения (класс) |
(C++17) |
поддержка хэширования для std::optional (специализация шаблона класса) |
(C++17) |
индикатор типа 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;
// индикатор состояния отсутствия значения
struct nullopt_t{/* смотрите описание */};
inline constexpr nullopt_t nullopt(/* неопределено */);
// класс 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, 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</* смотрите описание */> 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;
// конструкторы
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 = 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 = 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(/* смотрите описание */);
// наблюдатели
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> constexpr T value_or(U&&) const&;
template<class U> 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; // только для пояснения
};
template<class T>
optional(T) -> optional<T>;
}