[libcxx-commits] [libcxx] p3798: expected::has_error (PR #204826)
Alex Kremer via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Jun 19 06:48:00 PDT 2026
https://github.com/godexsoft created https://github.com/llvm/llvm-project/pull/204826
This PR attempts to start on the implementation of https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3798r1.html as it has been approved for C++29.
While doing this (first timer here) I could not find `c++29` or `c++2d` in the repository and I have no clue how to make the python script work with my changes to generate the feature flag tests correctly, etc.
I'm also leaving inline comments for other things I don't know and would need help with.
In case it's too early for c++29 support and to add `has_error` in particular, i'm happy to let it be until the codebase is ready.
Please send help as appropriate 🙏
>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] 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"],
},
{
More information about the libcxx-commits
mailing list