[libcxx-commits] [libcxx] [libc++] P3798R1: The unexpected in std::expected (PR #204826)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Jun 21 16:45:50 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Alex Kremer (godexsoft)
<details>
<summary>Changes</summary>
Closes #<!-- -->204394
Implements P3798 and related tests.
No AI was used to write the code.
---
Full diff: https://github.com/llvm/llvm-project/pull/204826.diff
4 Files Affected:
- (modified) libcxx/include/__expected/expected.h (+8)
- (added) libcxx/test/std/utilities/expected/expected.expected/observers/has_error.pass.cpp (+51)
- (added) libcxx/test/std/utilities/expected/expected.void/observers/has_error.pass.cpp (+51)
- (modified) libcxx/utils/generate_feature_test_macro_components.py (+1-1)
``````````diff
diff --git a/libcxx/include/__expected/expected.h b/libcxx/include/__expected/expected.h
index 32ed81a392702..360e9c4a8df6a 100644
--- a/libcxx/include/__expected/expected.h
+++ b/libcxx/include/__expected/expected.h
@@ -825,6 +825,10 @@ class expected : private __expected_base<_Tp, _Err> {
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__has_val(); }
+# if _LIBCPP_STD_VER >= 29
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_error() const noexcept { return !this->has_value(); }
+# endif
+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& value() const& {
static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");
if (!this->__has_val()) {
@@ -1599,6 +1603,10 @@ class expected<_Tp, _Err> : private __expected_void_base<_Err> {
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__has_val(); }
+# if _LIBCPP_STD_VER >= 29
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_error() const noexcept { return !this->has_value(); }
+# endif
+
_LIBCPP_HIDE_FROM_ABI constexpr void operator*() const noexcept {
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
this->__has_val(), "expected::operator* requires the expected to contain a value");
diff --git a/libcxx/test/std/utilities/expected/expected.expected/observers/has_error.pass.cpp b/libcxx/test/std/utilities/expected/expected.expected/observers/has_error.pass.cpp
new file mode 100644
index 0000000000000..80591aec36532
--- /dev/null
+++ b/libcxx/test/std/utilities/expected/expected.expected/observers/has_error.pass.cpp
@@ -0,0 +1,51 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++29
+
+// constexpr bool has_error() const noexcept;
+
+#include <cassert>
+#include <concepts>
+#include <expected>
+#include <type_traits>
+#include <utility>
+
+#include "../../types.h"
+
+static_assert(noexcept(std::expected<int, int>().has_error()));
+
+constexpr bool test() {
+ {
+ const std::expected<int, int> e(std::unexpect, 5);
+ assert(e.has_error());
+ }
+
+ {
+ const std::expected<int, int> e(5);
+ assert(!e.has_error());
+ }
+
+ return true;
+}
+
+constexpr bool test_nodiscard() {
+ std::expected<int, int> e;
+ e.has_error(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+ test_nodiscard();
+ static_assert(test_nodiscard());
+
+ return 0;
+}
diff --git a/libcxx/test/std/utilities/expected/expected.void/observers/has_error.pass.cpp b/libcxx/test/std/utilities/expected/expected.void/observers/has_error.pass.cpp
new file mode 100644
index 0000000000000..fdab2c39af6ee
--- /dev/null
+++ b/libcxx/test/std/utilities/expected/expected.void/observers/has_error.pass.cpp
@@ -0,0 +1,51 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++29
+
+// constexpr bool has_error() const noexcept;
+
+#include <cassert>
+#include <concepts>
+#include <expected>
+#include <type_traits>
+#include <utility>
+
+#include "../../types.h"
+
+static_assert(noexcept(std::expected<void, int>().has_error()));
+
+constexpr bool test() {
+ {
+ const std::expected<void, int> e(std::unexpect, 5);
+ assert(e.has_error());
+ }
+
+ {
+ const std::expected<void, int> e;
+ assert(!e.has_error());
+ }
+
+ return true;
+}
+
+constexpr bool test_nodiscard() {
+ std::expected<void, int> e;
+ e.has_error(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+ test_nodiscard();
+ static_assert(test_nodiscard());
+
+ return 0;
+}
diff --git a/libcxx/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py
index d764a1f677ba6..b8cc9113e8fb0 100644
--- a/libcxx/utils/generate_feature_test_macro_components.py
+++ b/libcxx/utils/generate_feature_test_macro_components.py
@@ -557,7 +557,7 @@ def add_version_header(tc):
},
{
"name": "__cpp_lib_expected",
- "values": {"c++23": 202211},
+ "values": {"c++23": 202211, "c++29": 202606},
"headers": ["expected"],
},
{
``````````
</details>
https://github.com/llvm/llvm-project/pull/204826
More information about the libcxx-commits
mailing list