[libcxx-commits] [libcxx] [libc++] P3798R1: The unexpected in std::expected (PR #204826)
Alex Kremer via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Jun 21 16:33:53 PDT 2026
https://github.com/godexsoft updated https://github.com/llvm/llvm-project/pull/204826
>From ba10238d9b8a57b9e3b8459021e8c0d5b3f5f0be Mon Sep 17 00:00:00 2001
From: Alex Kremer <akremer at ripple.com>
Date: Fri, 19 Jun 2026 14:38:21 +0100
Subject: [PATCH 1/3] Add basics for has_error
---
libcxx/include/__expected/expected.h | 4 ++
.../observers/has_error.pass.cpp | 57 +++++++++++++++++++
.../generate_feature_test_macro_components.py | 2 +-
3 files changed, 62 insertions(+), 1 deletion(-)
create mode 100644 libcxx/test/std/utilities/expected/expected.expected/observers/has_error.pass.cpp
diff --git a/libcxx/include/__expected/expected.h b/libcxx/include/__expected/expected.h
index 32ed81a392702..5b8d0cfc83923 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()) {
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..99607e9fc7a3f
--- /dev/null
+++ b/libcxx/test/std/utilities/expected/expected.expected/observers/has_error.pass.cpp
@@ -0,0 +1,57 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23, c++26
+
+// constexpr bool has_error() const noexcept;
+
+#include <cassert>
+#include <concepts>
+#include <expected>
+#include <type_traits>
+#include <utility>
+
+#include "test_macros.h"
+#include "../../types.h"
+
+// Test noexcept
+template <class T>
+concept HasErrorNoexcept =
+ requires(T t) {
+ { t.has_error() } noexcept;
+ };
+
+struct Foo {};
+static_assert(!HasErrorNoexcept<Foo>);
+
+static_assert(HasErrorNoexcept<std::expected<int, int>>);
+static_assert(HasErrorNoexcept<const std::expected<int, int>>);
+
+constexpr bool test() {
+ // has_error
+ {
+ const std::expected<int, int> e(std::unexpect, 5);
+ assert(e.has_error());
+ }
+
+ // !has_error
+ {
+ const std::expected<int, int> e(5);
+ assert(!e.has_error());
+ }
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+ 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"],
},
{
>From 21e19485738bf5f295c785fe92832de90e1ae321 Mon Sep 17 00:00:00 2001
From: Alex Kremer <akremer at ripple.com>
Date: Fri, 19 Jun 2026 16:03:27 +0100
Subject: [PATCH 2/3] Use preferred REQUIRES syntax
---
.../expected/expected.expected/observers/has_error.pass.cpp | 2 --
1 file changed, 2 deletions(-)
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
index 99607e9fc7a3f..a9c967b9331aa 100644
--- 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
@@ -8,8 +8,6 @@
// REQUIRES: std-at-least-c++29
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23, c++26
-
// constexpr bool has_error() const noexcept;
#include <cassert>
>From 8b3c605419b939199d5407ef2d51559424836573 Mon Sep 17 00:00:00 2001
From: Alex Kremer <akremer at ripple.com>
Date: Mon, 22 Jun 2026 00:33:21 +0100
Subject: [PATCH 3/3] Implement for void and review suggestions
---
libcxx/include/__expected/expected.h | 4 ++
.../observers/has_error.pass.cpp | 26 ++++------
.../observers/has_error.pass.cpp | 51 +++++++++++++++++++
3 files changed, 66 insertions(+), 15 deletions(-)
create mode 100644 libcxx/test/std/utilities/expected/expected.void/observers/has_error.pass.cpp
diff --git a/libcxx/include/__expected/expected.h b/libcxx/include/__expected/expected.h
index 5b8d0cfc83923..360e9c4a8df6a 100644
--- a/libcxx/include/__expected/expected.h
+++ b/libcxx/include/__expected/expected.h
@@ -1603,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
index a9c967b9331aa..80591aec36532 100644
--- 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
@@ -16,30 +16,16 @@
#include <type_traits>
#include <utility>
-#include "test_macros.h"
#include "../../types.h"
-// Test noexcept
-template <class T>
-concept HasErrorNoexcept =
- requires(T t) {
- { t.has_error() } noexcept;
- };
-
-struct Foo {};
-static_assert(!HasErrorNoexcept<Foo>);
-
-static_assert(HasErrorNoexcept<std::expected<int, int>>);
-static_assert(HasErrorNoexcept<const std::expected<int, int>>);
+static_assert(noexcept(std::expected<int, int>().has_error()));
constexpr bool test() {
- // has_error
{
const std::expected<int, int> e(std::unexpect, 5);
assert(e.has_error());
}
- // !has_error
{
const std::expected<int, int> e(5);
assert(!e.has_error());
@@ -48,8 +34,18 @@ constexpr bool test() {
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;
+}
More information about the libcxx-commits
mailing list