命名空间
变体
操作

标准库头文件 <expected> (C++23)

来自 cppreference.com
< cpp‎ | header
 
 
标准库头文件
语言支持
概念
<concepts> (C++20)
诊断
<system_error> (C++11)

内存管理
<memory_resource> (C++17)  
元编程
<type_traits> (C++11)
<ratio> (C++11)
通用实用程序
<utility>
<tuple> (C++11)
<optional> (C++17)
<variant> (C++17)
<any> (C++17)
<debugging> (C++26)
<expected> (C++23)
<bitset>
<charconv> (C++17)
<format> (C++20)
<bit> (C++20)

字符串
<cuchar> (C++11)

容器
<flat_set> (C++23)
<span> (C++20)
<mdspan> (C++23)
<inplace_vector> (C++26)
迭代器
<iterator>
范围
<ranges> (C++20)
<generator> (C++23)
算法
数值
<cfenv> (C++11)
<complex>
<cmath>
<linalg> (C++26)
<numbers> (C++20)

时间
<chrono> (C++11)
本地化
<codecvt> (C++11/17/26*)
<text_encoding> (C++26)
输入/输出
<filesystem> (C++17)
<cstdio>
<cinttypes> (C++11)
<strstream> (C++98/26*)
正则表达式
<regex> (C++11)
并发支持
<stop_token> (C++20)
<thread> (C++11)
<atomic> (C++11)
<rcu> (C++26)
<stdatomic.h> (C++23)
<mutex> (C++11)
<shared_mutex> (C++14)

<condition_variable> (C++11)  
<semaphore> (C++20)
<latch> (C++20)

<barrier> (C++20)
<future> (C++11)
<hazard_pointer> (C++26)

C 兼容性
<cstdbool> (C++11/17/20*)  
<ccomplex> (C++11/17/20*)
<ctgmath> (C++11/17/20*)

<cstdalign> (C++11/17/20*)

<ciso646> (直到 C++20)

 

此头文件是 通用实用程序 库的一部分。

内容

(C++23)
包含预期值或错误值的包装器
(类模板) [编辑]
表示为意外值
(类模板) [编辑]
异常,指示对包含意外值的 expected 进行检查访问
(类模板) [编辑]
expected 中意外值的原地构造标签
(标签)[编辑]

[编辑] 概要

namespace std {
  // class template unexpected
  template<class E> class unexpected;
 
  // class template bad_expected_access
  template<class E> class bad_expected_access;
 
  // specialization of bad_expected_access for void
  template<> class bad_expected_access<void>;
 
  // in-place construction of unexpected values
  struct unexpect_t {
    explicit unexpect_t() = default;
  };
  inline constexpr unexpect_t unexpect{};
 
  // class template expected
  template<class T, class E> class expected;
 
  // partial specialization of expected for void types
  template<class T, class E> requires is_void_v<T> class expected<T, E>;
}

[编辑] 类模板 std::unexpected

namespace std {
  template<class E>
  class unexpected {
  public:
    // constructors
    constexpr unexpected(const unexpected&) = default;
    constexpr unexpected(unexpected&&) = default;
    template<class... Args>
      constexpr explicit unexpected(in_place_t, Args&&...);
    template<class U, class... Args>
      constexpr explicit unexpected(in_place_t, initializer_list<U>, Args&&...);
    template<class Err = E>
      constexpr explicit unexpected(Err&&);
 
    // assignment
    constexpr unexpected& operator=(const unexpected&) = default;
    constexpr unexpected& operator=(unexpected&&) = default;
 
    // observer
    constexpr const E& error() const& noexcept;
    constexpr E& error() & noexcept;
    constexpr const E&& error() const&& noexcept;
    constexpr E&& error() && noexcept;
 
    // swap
    constexpr void swap(unexpected& other) noexcept(/* see description */);
 
    friend constexpr void swap(unexpected& x, unexpected& y) noexcept(noexcept(x.swap(y)));
 
    // equality operator
    template<class E2>
      friend constexpr bool operator==(const unexpected&, const unexpected<E2>&);
 
  private:
    E unex;    // exposition only
  };
 
  template<class E> 
    unexpected(E) -> unexpected<E>;
}

[编辑] 类模板 std::bad_expected_access

namespace std {
  template<class E>
  class bad_expected_access : public bad_expected_access<void> {
  public:
 
    // explicit constructor
    explicit bad_expected_access(E);
 
    // observers
    const char* what() const noexcept override;
    E& error() & noexcept;
    const E& error() const& noexcept;
    E&& error() && noexcept;
    const E&&  error() const&& noexcept;
 
  private:
    E unex;              // exposition only
  };
}

[编辑] 类模板特化 std::bad_expected_access<void>

namespace std {
  template<>
  class bad_expected_access<void> : public exception {
  protected:
    // constructors
    bad_expected_access() noexcept;
    bad_expected_access(const bad_expected_access&);
    bad_expected_access(bad_expected_access&&);
    bad_expected_access& operator=(const bad_expected_access&);
    bad_expected_access& operator=(bad_expected_access&&);
 
    ~bad_expected_access();
 
  public:
    const char* what() const noexcept override;
  };
}

[编辑] 类模板 std::expected

namespace std {
  template<class T, class E>
  class expected {
  public:
    using value_type = T;
    using error_type = E;
    using unexpected_type = unexpected<E>;
 
    template<class U>
    using rebind = expected<U, error_type>;
 
    // constructors
    constexpr expected();
    constexpr explicit(/* see description */) 
      expected(const expected&);
    constexpr explicit(/* see description */) 
      expected(expected&&) noexcept(/* see description */);
    template<class U, class G>
      constexpr explicit(/* see description */) expected(const expected<U, G>&);
    template<class U, class G>
      constexpr explicit(/* see description */) expected(expected<U, G>&&);
 
    template<class U = T>
      constexpr explicit(/* see description */) expected(U&& v);
 
    template<class G>
      constexpr expected(const unexpected<G>&);
    template<class G>
      constexpr expected(unexpected<G>&&);
 
    template<class... Args>
      constexpr explicit expected(in_place_t, Args&&...);
    template<class U, class... Args>
      constexpr explicit expected(in_place_t, initializer_list<U>, Args&&...);
    template<class... Args>
      constexpr explicit expected(unexpect_t, Args&&...);
    template<class U, class... Args>
      constexpr explicit expected(unexpect_t, initializer_list<U>, Args&&...);
 
    // destructor
    constexpr ~expected();
 
    // assignment
    constexpr expected& operator=(const expected&);
    constexpr expected& operator=(expected&&) noexcept(/* see description */);
    template<class U = T> constexpr expected& operator=(U&&);
    template<class G>
      constexpr expected& operator=(const unexpected<G>&);
    template<class G>
      constexpr expected& operator=(unexpected<G>&&);
 
    template<class... Args>
      constexpr T& emplace(Args&&...) noexcept;
    template<class U, class... Args>
      constexpr T& emplace(initializer_list<U>, Args&&...) noexcept;
 
    // swap
    constexpr void swap(expected&) noexcept(/* see description */);
    friend constexpr void swap(expected&, expected&) noexcept(/* see description */);
 
    // observers
    constexpr const T* operator->() const noexcept;
    constexpr T* operator->() noexcept;
    constexpr const T& operator*() const& noexcept;
    constexpr T& operator*() & noexcept;
    constexpr const T&& operator*() const&& noexcept;
    constexpr T&& operator*() && noexcept;
    constexpr explicit operator bool() const noexcept;
    constexpr bool has_value() const noexcept;
    constexpr const T& value() const&;
    constexpr T& value() &;
    constexpr const T&& value() const&&;
    constexpr T&& value() &&;
    constexpr const E& error() const&;
    constexpr E& error() &;
    constexpr const E&& error() const&&;
    constexpr E&& error() &&;
    template<class U> constexpr T value_or(U&&) const&;
    template<class U> constexpr T value_or(U&&) &&;
    template<class G = E> constexpr E error_or(G&&) const &;
    template<class G = E> constexpr E error_or(G&&) &&;
 
    // monadic operations
    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 or_else(F&& f) &;
    template<class F> constexpr auto or_else(F&& f) &&;
    template<class F> constexpr auto or_else(F&& f) const &;
    template<class F> constexpr auto or_else(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 auto transform_error(F&& f) &;
    template<class F> constexpr auto transform_error(F&& f) &&;
    template<class F> constexpr auto transform_error(F&& f) const &;
    template<class F> constexpr auto transform_error(F&& f) const &&;
 
    // equality operators
    template<class T2, class E2> requires (!is_void_v<T2>)
      friend constexpr bool operator==(const expected& x, const expected<T2, E2>& y);
    template<class T2>
      friend constexpr bool operator==(const expected&, const T2&);
    template<class E2>
      friend constexpr bool operator==(const expected&, const unexpected<E2>&);
 
  private:
    bool has_val;       // exposition only
    union {
      T val;            // exposition only
      E unex;           // exposition only
    };
  };
}

[编辑] void 类型的 std::expected 的部分特化

namespace std {
  template<class T, class E> requires is_void_v<T>
  class expected<T, E> {
  public:
    using value_type = T;
    using error_type = E;
    using unexpected_type = unexpected<E>;
 
    template<class U>
    using rebind = expected<U, error_type>;
 
    // constructors
    constexpr expected() noexcept;
    constexpr explicit(/* see description */)
      expected(const expected&);
    constexpr explicit(/* see description */)
      expected(expected&&) noexcept(/* see description */);
    template<class U, class G>
      constexpr explicit(/* see description */) expected(const expected<U, G>&);
    template<class U, class G>
      constexpr explicit(/* see description */) expected(expected<U, G>&&);
 
    template<class G>
      constexpr expected(const unexpected<G>&);
    template<class G>
      constexpr expected(unexpected<G>&&);
 
    constexpr explicit expected(in_place_t) noexcept;
    template<class... Args>
      constexpr explicit expected(unexpect_t, Args&&...);
    template<class U, class... Args>
      constexpr explicit expected(unexpect_t, initializer_list<U>, Args&&...);
 
    // destructor
    constexpr ~expected();
 
    // assignment
    constexpr expected& operator=(const expected&);
    constexpr expected& operator=(expected&&) noexcept(/* see description */);
    template<class G>
      constexpr expected& operator=(const unexpected<G>&);
    template<class G>
      constexpr expected& operator=(unexpected<G>&&);
    constexpr void emplace() noexcept;
 
    // swap
    constexpr void swap(expected&) noexcept(/* see description */);
    friend constexpr void swap(expected&, expected&) noexcept(/* see description */);
 
    // observers
    constexpr explicit operator bool() const noexcept;
    constexpr bool has_value() const noexcept;
    constexpr void operator*() const noexcept;
    constexpr void value() const&;
    constexpr void value() &&;
    constexpr const E& error() const&;
    constexpr E& error() &;
    constexpr const E&& error() const&&;
    constexpr E&& error() &&;
    template<class G = E> constexpr E error_or(G&&) const &;
    template<class G = E> constexpr E error_or(G&&) &&;
 
    // monadic operations
    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 or_else(F&& f) &;
    template<class F> constexpr auto or_else(F&& f) &&;
    template<class F> constexpr auto or_else(F&& f) const &;
    template<class F> constexpr auto or_else(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 auto transform_error(F&& f) &;
    template<class F> constexpr auto transform_error(F&& f) &&;
    template<class F> constexpr auto transform_error(F&& f) const &;
    template<class F> constexpr auto transform_error(F&& f) const &&;
 
    // equality operators
    template<class T2, class E2> requires is_void_v<T2>
      friend constexpr bool operator==(const expected& x, const expected<T2, E2>& y);
    template<class E2>
      friend constexpr bool operator==(const expected&, const unexpected<E2>&);
 
  private:
    bool has_val;        // exposition only
    union {
      E unex;            // exposition only
    };
  };
}