[libcxx-commits] [libcxx] [libc++] P3798R1: The unexpected in std::expected (PR #204826)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jun 21 18:47:54 PDT 2026


================
@@ -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;
+}
----------------
frederick-vs-ja wrote:

We generally don't test `[[nodiscard]]` in a `.pass.cpp`.

Please test `[[nodiscard]]` in `libcxx/test/libcxx/utilities/expected/nodiscard.verify.cpp` below this line:
https://github.com/llvm/llvm-project/blob/2e87cf8c2b8ec6453ccfa7e448d5b33f1d71a2ca/libcxx/test/libcxx/utilities/expected/nodiscard.verify.cpp#L49

https://github.com/llvm/llvm-project/pull/204826


More information about the libcxx-commits mailing list