标准库标头 <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 Err = E>
    constexpr explicit unexpected(Err&&);
    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&&...);
 
    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(/* 见说明 */);
 
    template<class E2>
    friend constexpr bool operator==(const unexpected&, const unexpected<E2>&);
 
    friend constexpr void swap(unexpected& x,
                               unexpected& y) noexcept(noexcept(x.swap(y)));
 
  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:
    constexpr explicit bad_expected_access(E);
    constexpr const char* what() const noexcept override;
    constexpr E& error() & noexcept;
    constexpr const E& error() const& noexcept;
    constexpr E&& error() && noexcept;
    constexpr const E&&  error() const&& noexcept;
 
  private:
    E /*unex*/; // 仅用于阐释
  };
}

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

namespace std {
  template<>
  class bad_expected_access<void> : public exception
  {
  protected:
    constexpr bad_expected_access() noexcept;
    constexpr bad_expected_access(const bad_expected_access&);
    constexpr bad_expected_access(bad_expected_access&&);
    constexpr bad_expected_access& operator=(const bad_expected_access&);
    constexpr bad_expected_access& operator=(bad_expected_access&&);
    constexpr ~bad_expected_access();
 
  public:
    constexpr 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 expected(const expected&);
    constexpr 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 explicit(/* 见说明 */) expected(const unexpected<G>&);
    template<class G>
    constexpr explicit(/* 见说明 */) 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& x, expected& y) noexcept(noexcept(x.swap(y)));
 
    // 观察器
    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& noexcept;
    constexpr E& error() & noexcept;
    constexpr const E&& error() const&& noexcept;
    constexpr E&& error() && noexcept;
    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 类型的部分特化

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 expected(const expected&);
  constexpr 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 explicit(/* 见说明 */) expected(const unexpected<G>&);
  template<class G>
  constexpr explicit(/* 见说明 */) 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& x, expected& y) noexcept(noexcept(x.swap(y)));
 
  // 观察器
  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& noexept;
  constexpr E& error() & noexcept;
  constexpr const E&& error() const&& noexcept;
  constexpr E&& error() && noexcept;
  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 E2>
  friend constexpr bool operator==(const expected&, const unexpected<E2>&);
 
private:
  bool /*has-val*/; // 仅用于解释
  union
  {
    E /*unex*/; // 仅用于解释
  };
};