Namespaces
Variants

Standard library header <optional> (C++17)

From cppreference.net
Standard library headers

このヘッダは 汎用ユーティリティ ライブラリの一部です。

目次

インクルード

(C++20)
三方比較演算子 サポート

クラス

(C++17)
オブジェクトを保持する場合と保持しない場合があるラッパー
(クラステンプレート)
値を含まないoptionalへのチェック付きアクセスを示す例外
(クラス)
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 オブジェクトを比較する
(関数テンプレート)
特殊化アルゴリズム
std::swap アルゴリズムを特殊化する
(関数テンプレート)
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) { // 説明専用
    []<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();
    // assignment
    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; // 説明専用
  };
  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;
  };
}