Namespaces
Variants
Views
Actions

Standard library header <optional>

From cppreference.com
< cpp‎ | header
Revision as of 02:26, 23 June 2013 by Bazzy (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This header is part of the general utility library.


Template:cpp/utility/optional/dsc hashTemplate:cpp/utility/optional/dsc operator cmpTemplate:cpp/utility/optional/dsc make optionalTemplate:cpp/utility/optional/dsc swap2

Contents

Includes

<initializer_list>

Classes

(C++14)
a wrapper that may or may not hold an object
(class template) [edit]
exception indicating access to a optional type with uninitialized state
(class) [edit]
disambiguation tag type for in-place construction of optional types
(class) [edit]
(C++14)
indicator of optional type with uninitialized state
(class) [edit]

Functions

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