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

来自cppreference.com
< cpp‎ | header


 
 
标准库头
通用工具
<any> (C++17)
<bitset>
<bit> (C++20)
<charconv> (C++17)
<expected> (C++23)
<format> (C++20)
<functional>
<optional> (C++17)
<stdbit.h> (C++26)
<tuple> (C++11)
<typeindex> (C++11)
<utility>
<variant> (C++17)
容器
<array> (C++11)
<deque>
<flat_map> (C++23)
<flat_set> (C++23)
<forward_list> (C++11)
<hive> (C++26)
<inplace_vector> (C++26)   
<list>
<map>
<mdspan> (C++23)
<queue>
<set>
<span> (C++20)
<stack>
<unordered_map> (C++11)
<unordered_set> (C++11)
<vector>
迭代器
<iterator>
范围
<generator> (C++23)
<ranges> (C++20)
 

此标头是工具库的一部分。

目录

(C++23)
含有一个预期值或错误值的包装器
(类模板) [编辑]
表示一个非预期值
(类模板) [编辑]
指示对含有非预期值的 expected 的有检查访问的异常
(类模板) [编辑]
expected 中非预期值的原位构造标签
(类) (常量) [编辑]

[编辑] 概要

namespace std {
  // 类模板 unexpected
  template<class E> class unexpected;
 
  // 类模板 bad_expected_access
  template<class E> class bad_expected_access
 
  // bad_expected_access 的 void 特化
  template<> class bad_expected_access<void>;
 
  // unexpected 值的原地构造
  struct unexpect_t {
    explicit unexpect_t() = default;
  };
  inline constexpr unexpect_t unexpect{};
 
  // 类模板 expected
  template<class T, class E> class expected;
 
  // expected 的 void 类型的部分特化
  template<class T, class E> requires is_void_v<T> class expected<T, E>;
}

[编辑] 类模板 std::unexpected

namespace std {
  template<class E>
  class unexpected {
  public:
    // 构造
    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&&);
 
    // 赋值
    constexpr unexpected& operator=(const unexpected&) = default;
    constexpr unexpected& operator=(unexpected&&) = default;
 
    // 观察器
    constexpr const E& error() const& noexcept;
    constexpr E& error() & noexcept;
    constexpr const E&& error() const&& noexcept;
    constexpr E&& error() && noexcept;
 
    // 交换
    constexpr void swap(unexpected& other) noexcept(/* 见说明 */);
 
    friend constexpr void swap(unexpected& x, unexpected& y) noexcept(noexcept(x.swap(y)));
 
    // 相等运算
    template<class E2>
      friend constexpr bool operator==(const unexpected&, const unexpected<E2>&);
 
  private:
    E unex;    // 仅用于阐释
  };
 
  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 bad_expected_access(E);
 
    // 观察器
    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;              // 仅用于阐释
  };
}

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

namespace std {
  template<>
  class bad_expected_access<void> : public exception {
  protected:
    // 构造
    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>;
 
    // 构造
    constexpr expected();
    constexpr explicit(/* 见说明 */) 
      expected(const expected&);
    constexpr explicit(/* 见说明 */) 
      expected(expected&&) noexcept(/* 见说明 */);
    template<class U, class G>
      constexpr explicit(/* 见说明 */) expected(const expected<U, G>&);
    template<class U, class G>
      constexpr explicit(/* 见说明 */) expected(expected<U, G>&&);
 
    template<class U = remove_cv_t<T>>
      constexpr explicit(/* 见说明 */) 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&&...);
 
    // 析构
    constexpr ~expected();
 
    // 赋值
    constexpr expected& operator=(const expected&);
    constexpr expected& operator=(expected&&) noexcept(/* 见说明 */);
    template<class U = remove_cv_t<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;
 
    // 交换
    constexpr void swap(expected&) noexcept(/* 见说明 */);
    friend constexpr void swap(expected&, expected&) noexcept(/* 见说明 */);
 
    // 观察器
    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 = remove_cv_t<T>> constexpr T value_or(U&&) const&;
    template<class U = remove_cv_t<T>> constexpr T value_or(U&&) &&;
    template<class G = E> constexpr E error_or(G&&) const &;
    template<class G = E> constexpr E error_or(G&&) &&;
 
    // 单子操作
    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 &&;
 
    // 相等运算
    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;       // 仅用于解释
    union {
      T val;            // 仅用于解释
      E unex;           // 仅用于解释
    };
  };
}

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

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>;
 
    // 构造
    constexpr expected() noexcept;
    constexpr explicit(/* 见说明 */)
      expected(const expected&);
    constexpr explicit(/* 见说明 */)
      expected(expected&&) noexcept(/* 见说明 */);
    template<class U, class G>
      constexpr explicit(/* 见说明 */) expected(const expected<U, G>&);
    template<class U, class G>
      constexpr explicit(/* 见说明 */) 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&&...);
 
    // 析构
    constexpr ~expected();
 
    // 赋值
    constexpr expected& operator=(const expected&);
    constexpr expected& operator=(expected&&) noexcept(/* 见说明 */);
    template<class G>
      constexpr expected& operator=(const unexpected<G>&);
    template<class G>
      constexpr expected& operator=(unexpected<G>&&);
    constexpr void emplace() noexcept;
 
    // 交换
    constexpr void swap(expected&) noexcept(/* 见说明 */);
    friend constexpr void swap(expected&, expected&) noexcept(/* 见说明 */);
 
    // 观察器
    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 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 &&;
 
    // 相等运算
    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;        // 仅用于解释
    union {
      E unex;            // 仅用于解释
    };
  };
}