标准库标头 <expected> (C++23)
来自cppreference.com
此标头是工具库的一部分。
类 | |
(C++23) |
含有一个预期值或错误值的包装器 (类模板) |
(C++23) |
表示一个非预期值 (类模板) |
(C++23) |
指示对含有非预期值的 expected 的有检查访问的异常 (类模板) |
(C++23) |
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>;
}
类模板 cpp/header/expected/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::expected 的 void 类型的部分特化
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; // 仅用于解释
};
};
}