Standard library header <optional>
From cppreference.com
This header is part of the general utility library.
Includes | |
| <initializer_list> | |
Classes | |
(C++17) |
a wrapper that may or may not hold an object (class template) |
(since C++17) |
exception indicating checked access to an optional that doesn't contain a value (class) |
(C++17) |
hash support for std::optional (class template specialization) |
(since C++17) |
indicator of optional type with uninitialized state (class) |
Functions | |
(C++17)(C++17)(C++17)(C++17)(C++17)(C++17)(C++20) |
compares optional objects (function template) |
(C++17) |
creates an optional object (function template) |
(C++17) |
specializes the std::swap algorithm (function template) |
Synopsis
#include <initializer_list>
namespace std {
// 20.6.4, optional for object types
template <class T> class optional;
// 20.6.5, In-place construction
struct in_place_t{};
constexpr in_place_t in_place{};
// 20.6.6, Disengaged state indicator
struct nullopt_t;
constexpr nullopt_t nullopt( /*unspecified*/ );
// 20.6.7, class bad_optional_access
class bad_optional_access;
// 20.6.8, 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>&);
// 20.6.9, 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;
// 20.6.10, 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>&);
// 20.6.11, Specialized algorithms
template <class T> void swap(optional<T>&, optional<T>&) noexcept(see below );
template <class T> constexpr optional<T> make_optional(T&&);
// 20.6.12, hash support
template <class T> struct hash;
template <class T> struct hash<optional<T>>;
} // namespace std
Class std::optional
template <class T>
class optional
{
public:
typedef T value_type;
// 20.6.4.1, constructors
constexpr optional() noexcept;
constexpr optional(nullopt_t) noexcept;
optional(const optional&);
optional(optional&&) noexcept(see below );
constexpr optional(const T&);
constexpr optional(T&&);
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&&...);
// 20.6.4.2, destructor
~optional();
// 20.6.4.3, assignment
optional& operator=(nullopt_t) noexcept;
optional& operator=(const optional&);
optional& operator=(optional&&) noexcept(see below );
template <class U> optional& operator=(U&&);
template <class... Args> void emplace(Args&&...);
template <class U, class... Args> void emplace(initializer_list<U>, Args&&...);
// 20.6.4.4, swap
void swap(optional&) noexcept(see below );
// 20.6.4.5, observers
constexpr T const* operator->() const;
T* operator->();
constexpr T const& operator*() const;
T& operator*();
constexpr explicit operator bool() const noexcept;
constexpr T const& value() const;
T& value();
template <class U> constexpr T value_or(U&&) const&;
template <class U> T value_or(U&&) &&;
private:
bool init; // exposition only
T* val; // exposition only
};