[libcxx-commits] [libcxx] [libc++][optional] LWG4497: `std::nullopt_t` should be comparable (PR #214975)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Sun Aug 9 19:06:55 PDT 2026


================
@@ -0,0 +1,102 @@
+//===----------------------------------------------------------------------===//
+//
+// 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++17
+
+// <optional>
+
+// struct nullopt_t{see below};
+// inline constexpr nullopt_t nullopt(unspecified);
+
+// [optional.nullopt]/2:
+//  nullopt_t models copyable and three_way_comparable<strong_ordering>.
+
+#include <algorithm>
+#include <cassert>
+#include <concepts>
+#include <optional>
+#include <vector>
+
+#include "test_macros.h"
+
+#if TEST_STD_VER >= 20
+static_assert(std::copyable<std::nullopt_t>);
+static_assert(std::three_way_comparable<std::nullopt_t, std::strong_ordering>);
+#endif
+
+constexpr bool test() {
+  { // ==
+    auto result = (std::nullopt == std::nullopt);
+    static_assert(std::is_same_v<decltype(result), bool>);
+    static_assert(noexcept(std::nullopt == std::nullopt));
----------------
frederick-vs-ja wrote:

This should be fine but the standard requirements looks a bit too loose, again.

Currently, the standard wording doesn't say anything about `noexcept`-ness for comparisons of `nullopt_t`. Although it's obviously better to me to make the comparisons `noexcept`.

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


More information about the libcxx-commits mailing list