[libcxx-commits] [libcxx] [libc++][test] Test SFINAE for `optional`'s operators in C++17 (PR #221643)

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Sep 6 22:55:54 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: A. Jiang (frederick-vs-ja)

<details>
<summary>Changes</summary>

We have been intentionally constraining `optional`'s comparison operators since C++17, so it is probably better to test the constraints in C++17 mode. Given the constraints are standardized only since C++26 (via P2944R3), `LIBCPP_STATIC_ASSERT` is used in old modes.

As drive-by, also switches to use `// REQUIRES: std-at-least-c++17` in touched test files, and adds previously missed includes of `<type_traits>`.

---

Patch is 34.98 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/221643.diff


13 Files Affected:

- (modified) libcxx/test/std/utilities/optional/optional.comp_with_t/equal.pass.cpp (+19-16) 
- (modified) libcxx/test/std/utilities/optional/optional.comp_with_t/greater.pass.cpp (+19-16) 
- (modified) libcxx/test/std/utilities/optional/optional.comp_with_t/greater_equal.pass.cpp (+19-16) 
- (modified) libcxx/test/std/utilities/optional/optional.comp_with_t/less_equal.pass.cpp (+19-16) 
- (modified) libcxx/test/std/utilities/optional/optional.comp_with_t/less_than.pass.cpp (+19-16) 
- (modified) libcxx/test/std/utilities/optional/optional.comp_with_t/not_equal.pass.cpp (+19-16) 
- (modified) libcxx/test/std/utilities/optional/optional.relops/equal.pass.cpp (+11-9) 
- (modified) libcxx/test/std/utilities/optional/optional.relops/greater_equal.pass.cpp (+11-8) 
- (modified) libcxx/test/std/utilities/optional/optional.relops/greater_than.pass.cpp (+11-8) 
- (modified) libcxx/test/std/utilities/optional/optional.relops/less_equal.pass.cpp (+10-8) 
- (modified) libcxx/test/std/utilities/optional/optional.relops/less_than.pass.cpp (+10-8) 
- (modified) libcxx/test/std/utilities/optional/optional.relops/not_equal.pass.cpp (+11-9) 
- (modified) libcxx/test/support/test_comparisons.h (+32) 


``````````diff
diff --git a/libcxx/test/std/utilities/optional/optional.comp_with_t/equal.pass.cpp b/libcxx/test/std/utilities/optional/optional.comp_with_t/equal.pass.cpp
index 99813dae89031..d33caab090b3b 100644
--- a/libcxx/test/std/utilities/optional/optional.comp_with_t/equal.pass.cpp
+++ b/libcxx/test/std/utilities/optional/optional.comp_with_t/equal.pass.cpp
@@ -6,18 +6,23 @@
 //
 //===----------------------------------------------------------------------===//
 
-// UNSUPPORTED: c++03, c++11, c++14
+// REQUIRES: std-at-least-c++17
 // <optional>
 
 // template <class T, class U> constexpr bool operator==(const optional<T>& x, const U& v);
 // template <class T, class U> constexpr bool operator==(const U& v, const optional<T>& x);
 
 #include <optional>
+#include <type_traits>
 
 #include "test_comparisons.h"
 #include "test_macros.h"
 
 #if TEST_STD_VER >= 26
+#  define STATIC_ASSERT_OPTIONAL_CMP static_assert
+#else
+#  define STATIC_ASSERT_OPTIONAL_CMP LIBCPP_STATIC_ASSERT
+#endif
 
 struct tester {};
 
@@ -32,7 +37,7 @@ constexpr bool operator==(const T& t, const tester& a) {
 
 template <class T, class U>
 constexpr std::optional<bool> try_cmp_eq(const T& t, const U& u) {
-  if constexpr (requires { t == u; })
+  if constexpr (HasOperatorEqual<const T, const U>)
     return t == u;
   else
     return {};
@@ -40,25 +45,23 @@ constexpr std::optional<bool> try_cmp_eq(const T& t, const U& u) {
 
 // Test SFINAE.
 
-static_assert(HasOperatorEqual<int, std::optional<int>>);
-static_assert(HasOperatorEqual<int, std::optional<EqualityComparable>>);
-static_assert(HasOperatorEqual<EqualityComparable, std::optional<EqualityComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorEqual<int, std::optional<int>>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorEqual<int, std::optional<EqualityComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorEqual<EqualityComparable, std::optional<EqualityComparable>>);
 
-static_assert(!HasOperatorEqual<NonComparable, std::optional<NonComparable>>);
-static_assert(!HasOperatorEqual<NonComparable, std::optional<EqualityComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorEqual<NonComparable, std::optional<NonComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorEqual<NonComparable, std::optional<EqualityComparable>>);
 
-static_assert(HasOperatorEqual<std::optional<int>, int>);
-static_assert(HasOperatorEqual<std::optional<EqualityComparable>, int>);
-static_assert(HasOperatorEqual<std::optional<EqualityComparable>, EqualityComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorEqual<std::optional<int>, int>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorEqual<std::optional<EqualityComparable>, int>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorEqual<std::optional<EqualityComparable>, EqualityComparable>);
 
-static_assert(!HasOperatorEqual<std::optional<NonComparable>, NonComparable>);
-static_assert(!HasOperatorEqual<std::optional<EqualityComparable>, NonComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorEqual<std::optional<NonComparable>, NonComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorEqual<std::optional<EqualityComparable>, NonComparable>);
 
 // LWG4072: avoid ambiguity with optional's own comparison operators
-static_assert(try_cmp_eq(std::optional<int>{}, std::optional<tester>{}) == std::optional<bool>{});
-static_assert(try_cmp_eq(std::optional<int>{}, std::optional<int>{}) == std::optional<bool>{true});
-
-#endif
+STATIC_ASSERT_OPTIONAL_CMP(try_cmp_eq(std::optional<int>{}, std::optional<tester>{}) == std::optional<bool>{});
+STATIC_ASSERT_OPTIONAL_CMP(try_cmp_eq(std::optional<int>{}, std::optional<int>{}) == std::optional<bool>{true});
 
 using std::optional;
 
diff --git a/libcxx/test/std/utilities/optional/optional.comp_with_t/greater.pass.cpp b/libcxx/test/std/utilities/optional/optional.comp_with_t/greater.pass.cpp
index a945cf4b54002..f04a5fb8d451a 100644
--- a/libcxx/test/std/utilities/optional/optional.comp_with_t/greater.pass.cpp
+++ b/libcxx/test/std/utilities/optional/optional.comp_with_t/greater.pass.cpp
@@ -6,18 +6,23 @@
 //
 //===----------------------------------------------------------------------===//
 
-// UNSUPPORTED: c++03, c++11, c++14
+// REQUIRES: std-at-least-c++17
 // <optional>
 
 // template <class T, class U> constexpr bool operator>(const optional<T>& x, const U& v);
 // template <class T, class U> constexpr bool operator>(const U& v, const optional<T>& x);
 
 #include <optional>
+#include <type_traits>
 
 #include "test_comparisons.h"
 #include "test_macros.h"
 
 #if TEST_STD_VER >= 26
+#  define STATIC_ASSERT_OPTIONAL_CMP static_assert
+#else
+#  define STATIC_ASSERT_OPTIONAL_CMP LIBCPP_STATIC_ASSERT
+#endif
 
 struct tester {};
 
@@ -32,32 +37,30 @@ constexpr bool operator>(const T& t, const tester& a) {
 
 template <class T, class U>
 constexpr std::optional<bool> try_cmp_gt(const T& t, const U& u) {
-  if constexpr (requires { t > u; })
+  if constexpr (HasOperatorGreaterThan<const T, const U>)
     return t > u;
   else
     return {};
 }
 
 // Test SFINAE.
-static_assert(HasOperatorGreaterThan<std::optional<ThreeWayComparable>, int>);
-static_assert(HasOperatorGreaterThan<std::optional<ThreeWayComparable>, ThreeWayComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorGreaterThan<std::optional<ThreeWayComparable>, int>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorGreaterThan<std::optional<ThreeWayComparable>, ThreeWayComparable>);
 
-static_assert(!HasOperatorGreaterThan<std::optional<NonComparable>, NonComparable>);
-static_assert(!HasOperatorGreaterThan<std::optional<ThreeWayComparable>, NonComparable>);
-static_assert(!HasOperatorGreaterThan<std::optional<NonComparable>, ThreeWayComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorGreaterThan<std::optional<NonComparable>, NonComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorGreaterThan<std::optional<ThreeWayComparable>, NonComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorGreaterThan<std::optional<NonComparable>, ThreeWayComparable>);
 
-static_assert(HasOperatorGreaterThan<int, std::optional<ThreeWayComparable>>);
-static_assert(HasOperatorGreaterThan<ThreeWayComparable, std::optional<ThreeWayComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorGreaterThan<int, std::optional<ThreeWayComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorGreaterThan<ThreeWayComparable, std::optional<ThreeWayComparable>>);
 
-static_assert(!HasOperatorGreaterThan<NonComparable, std::optional<NonComparable>>);
-static_assert(!HasOperatorGreaterThan<NonComparable, std::optional<ThreeWayComparable>>);
-static_assert(!HasOperatorGreaterThan<ThreeWayComparable, std::optional<NonComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorGreaterThan<NonComparable, std::optional<NonComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorGreaterThan<NonComparable, std::optional<ThreeWayComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorGreaterThan<ThreeWayComparable, std::optional<NonComparable>>);
 
 // LWG4072: avoid ambiguity with optional's own comparison operators
-static_assert(try_cmp_gt(std::optional<int>{}, std::optional<tester>{}) == std::optional<bool>{});
-static_assert(try_cmp_gt(std::optional<int>{}, std::optional<int>{}) == std::optional<bool>{false});
-
-#endif
+STATIC_ASSERT_OPTIONAL_CMP(try_cmp_gt(std::optional<int>{}, std::optional<tester>{}) == std::optional<bool>{});
+STATIC_ASSERT_OPTIONAL_CMP(try_cmp_gt(std::optional<int>{}, std::optional<int>{}) == std::optional<bool>{false});
 
 using std::optional;
 
diff --git a/libcxx/test/std/utilities/optional/optional.comp_with_t/greater_equal.pass.cpp b/libcxx/test/std/utilities/optional/optional.comp_with_t/greater_equal.pass.cpp
index 8e18568bfe6d7..c8cde4562d755 100644
--- a/libcxx/test/std/utilities/optional/optional.comp_with_t/greater_equal.pass.cpp
+++ b/libcxx/test/std/utilities/optional/optional.comp_with_t/greater_equal.pass.cpp
@@ -6,18 +6,23 @@
 //
 //===----------------------------------------------------------------------===//
 
-// UNSUPPORTED: c++03, c++11, c++14
+// REQUIRES: std-at-least-c++17
 // <optional>
 
 // template <class T, class U> constexpr bool operator>=(const optional<T>& x, const U& v);
 // template <class T, class U> constexpr bool operator>=(const U& v, const optional<T>& x);
 
 #include <optional>
+#include <type_traits>
 
 #include "test_comparisons.h"
 #include "test_macros.h"
 
 #if TEST_STD_VER >= 26
+#  define STATIC_ASSERT_OPTIONAL_CMP static_assert
+#else
+#  define STATIC_ASSERT_OPTIONAL_CMP LIBCPP_STATIC_ASSERT
+#endif
 
 struct tester {};
 
@@ -32,32 +37,30 @@ constexpr bool operator>=(const T& t, const tester& a) {
 
 template <class T, class U>
 constexpr std::optional<bool> try_cmp_gteq(const T& t, const U& u) {
-  if constexpr (requires { t >= u; })
+  if constexpr (HasOperatorGreaterThanEqual<const T, const U>)
     return t >= u;
   else
     return {};
 }
 
 // Test SFINAE.
-static_assert(HasOperatorGreaterThanEqual<std::optional<ThreeWayComparable>, int>);
-static_assert(HasOperatorGreaterThanEqual<std::optional<ThreeWayComparable>, ThreeWayComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorGreaterThanEqual<std::optional<ThreeWayComparable>, int>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorGreaterThanEqual<std::optional<ThreeWayComparable>, ThreeWayComparable>);
 
-static_assert(!HasOperatorGreaterThanEqual<std::optional<NonComparable>, NonComparable>);
-static_assert(!HasOperatorGreaterThanEqual<std::optional<ThreeWayComparable>, NonComparable>);
-static_assert(!HasOperatorGreaterThanEqual<std::optional<NonComparable>, ThreeWayComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorGreaterThanEqual<std::optional<NonComparable>, NonComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorGreaterThanEqual<std::optional<ThreeWayComparable>, NonComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorGreaterThanEqual<std::optional<NonComparable>, ThreeWayComparable>);
 
-static_assert(HasOperatorGreaterThanEqual<int, std::optional<ThreeWayComparable>>);
-static_assert(HasOperatorGreaterThanEqual<ThreeWayComparable, std::optional<ThreeWayComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorGreaterThanEqual<int, std::optional<ThreeWayComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorGreaterThanEqual<ThreeWayComparable, std::optional<ThreeWayComparable>>);
 
-static_assert(!HasOperatorGreaterThanEqual<NonComparable, std::optional<NonComparable>>);
-static_assert(!HasOperatorGreaterThanEqual<NonComparable, std::optional<ThreeWayComparable>>);
-static_assert(!HasOperatorGreaterThanEqual<ThreeWayComparable, std::optional<NonComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorGreaterThanEqual<NonComparable, std::optional<NonComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorGreaterThanEqual<NonComparable, std::optional<ThreeWayComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorGreaterThanEqual<ThreeWayComparable, std::optional<NonComparable>>);
 
 // LWG4072: avoid ambiguity with optional's own comparison operators
-static_assert(try_cmp_gteq(std::optional<int>{}, std::optional<tester>{}) == std::optional<bool>{});
-static_assert(try_cmp_gteq(std::optional<int>{}, std::optional<int>{}) == std::optional<bool>{true});
-
-#endif
+STATIC_ASSERT_OPTIONAL_CMP(try_cmp_gteq(std::optional<int>{}, std::optional<tester>{}) == std::optional<bool>{});
+STATIC_ASSERT_OPTIONAL_CMP(try_cmp_gteq(std::optional<int>{}, std::optional<int>{}) == std::optional<bool>{true});
 
 using std::optional;
 
diff --git a/libcxx/test/std/utilities/optional/optional.comp_with_t/less_equal.pass.cpp b/libcxx/test/std/utilities/optional/optional.comp_with_t/less_equal.pass.cpp
index c937d7b2a5faf..593acd9af12bf 100644
--- a/libcxx/test/std/utilities/optional/optional.comp_with_t/less_equal.pass.cpp
+++ b/libcxx/test/std/utilities/optional/optional.comp_with_t/less_equal.pass.cpp
@@ -6,18 +6,23 @@
 //
 //===----------------------------------------------------------------------===//
 
-// UNSUPPORTED: c++03, c++11, c++14
+// REQUIRES: std-at-least-c++17
 // <optional>
 
 // template <class T, class U> constexpr bool operator<=(const optional<T>& x, const U& v);
 // template <class T, class U> constexpr bool operator<=(const U& v, const optional<T>& x);
 
 #include <optional>
+#include <type_traits>
 
 #include "test_comparisons.h"
 #include "test_macros.h"
 
 #if TEST_STD_VER >= 26
+#  define STATIC_ASSERT_OPTIONAL_CMP static_assert
+#else
+#  define STATIC_ASSERT_OPTIONAL_CMP LIBCPP_STATIC_ASSERT
+#endif
 
 struct tester {};
 
@@ -32,32 +37,30 @@ constexpr bool operator<=(const T& t, const tester& a) {
 
 template <class T, class U>
 constexpr std::optional<bool> try_cmp_lteq(const T& t, const U& u) {
-  if constexpr (requires { t <= u; })
+  if constexpr (HasOperatorLessThanEqual<const T, const U>)
     return t <= u;
   else
     return {};
 }
 
 // Test SFINAE.
-static_assert(HasOperatorLessThanEqual<std::optional<ThreeWayComparable>, int>);
-static_assert(HasOperatorLessThanEqual<std::optional<ThreeWayComparable>, ThreeWayComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorLessThanEqual<std::optional<ThreeWayComparable>, int>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorLessThanEqual<std::optional<ThreeWayComparable>, ThreeWayComparable>);
 
-static_assert(!HasOperatorLessThanEqual<std::optional<NonComparable>, NonComparable>);
-static_assert(!HasOperatorLessThanEqual<std::optional<ThreeWayComparable>, NonComparable>);
-static_assert(!HasOperatorLessThanEqual<std::optional<NonComparable>, ThreeWayComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorLessThanEqual<std::optional<NonComparable>, NonComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorLessThanEqual<std::optional<ThreeWayComparable>, NonComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorLessThanEqual<std::optional<NonComparable>, ThreeWayComparable>);
 
-static_assert(HasOperatorLessThanEqual<int, std::optional<ThreeWayComparable>>);
-static_assert(HasOperatorLessThanEqual<ThreeWayComparable, std::optional<ThreeWayComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorLessThanEqual<int, std::optional<ThreeWayComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorLessThanEqual<ThreeWayComparable, std::optional<ThreeWayComparable>>);
 
-static_assert(!HasOperatorLessThanEqual<NonComparable, std::optional<NonComparable>>);
-static_assert(!HasOperatorLessThanEqual<NonComparable, std::optional<ThreeWayComparable>>);
-static_assert(!HasOperatorLessThanEqual<ThreeWayComparable, std::optional<NonComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorLessThanEqual<NonComparable, std::optional<NonComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorLessThanEqual<NonComparable, std::optional<ThreeWayComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorLessThanEqual<ThreeWayComparable, std::optional<NonComparable>>);
 
 // LWG4072: avoid ambiguity with optional's own comparison operators
-static_assert(try_cmp_lteq(std::optional<int>{}, std::optional<tester>{}) == std::optional<bool>{});
-static_assert(try_cmp_lteq(std::optional<int>{}, std::optional<int>{}) == std::optional<bool>{true});
-
-#endif
+STATIC_ASSERT_OPTIONAL_CMP(try_cmp_lteq(std::optional<int>{}, std::optional<tester>{}) == std::optional<bool>{});
+STATIC_ASSERT_OPTIONAL_CMP(try_cmp_lteq(std::optional<int>{}, std::optional<int>{}) == std::optional<bool>{true});
 
 using std::optional;
 
diff --git a/libcxx/test/std/utilities/optional/optional.comp_with_t/less_than.pass.cpp b/libcxx/test/std/utilities/optional/optional.comp_with_t/less_than.pass.cpp
index 9a1aa0c65f272..a898b5182ff9e 100644
--- a/libcxx/test/std/utilities/optional/optional.comp_with_t/less_than.pass.cpp
+++ b/libcxx/test/std/utilities/optional/optional.comp_with_t/less_than.pass.cpp
@@ -6,18 +6,23 @@
 //
 //===----------------------------------------------------------------------===//
 
-// UNSUPPORTED: c++03, c++11, c++14
+// REQUIRES: std-at-least-c++17
 // <optional>
 
 // template <class T, class U> constexpr bool operator<(const optional<T>& x, const U& v);
 // template <class T, class U> constexpr bool operator<(const U& v, const optional<T>& x);
 
 #include <optional>
+#include <type_traits>
 
 #include "test_comparisons.h"
 #include "test_macros.h"
 
 #if TEST_STD_VER >= 26
+#  define STATIC_ASSERT_OPTIONAL_CMP static_assert
+#else
+#  define STATIC_ASSERT_OPTIONAL_CMP LIBCPP_STATIC_ASSERT
+#endif
 
 struct tester {};
 
@@ -32,32 +37,30 @@ constexpr bool operator<(const T& t, const tester& a) {
 
 template <class T, class U>
 constexpr std::optional<bool> try_cmp_lt(const T& t, const U& u) {
-  if constexpr (requires { t < u; })
+  if constexpr (HasOperatorLessThan<const T, const U>)
     return t < u;
   else
     return {};
 }
 
 // Test SFINAE.
-static_assert(HasOperatorLessThan<std::optional<ThreeWayComparable>, int>);
-static_assert(HasOperatorLessThan<std::optional<ThreeWayComparable>, ThreeWayComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorLessThan<std::optional<ThreeWayComparable>, int>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorLessThan<std::optional<ThreeWayComparable>, ThreeWayComparable>);
 
-static_assert(!HasOperatorLessThan<std::optional<NonComparable>, NonComparable>);
-static_assert(!HasOperatorLessThan<std::optional<ThreeWayComparable>, NonComparable>);
-static_assert(!HasOperatorLessThan<std::optional<NonComparable>, ThreeWayComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorLessThan<std::optional<NonComparable>, NonComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorLessThan<std::optional<ThreeWayComparable>, NonComparable>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorLessThan<std::optional<NonComparable>, ThreeWayComparable>);
 
-static_assert(HasOperatorLessThan<int, std::optional<ThreeWayComparable>>);
-static_assert(HasOperatorLessThan<ThreeWayComparable, std::optional<ThreeWayComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorLessThan<int, std::optional<ThreeWayComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(HasOperatorLessThan<ThreeWayComparable, std::optional<ThreeWayComparable>>);
 
-static_assert(!HasOperatorLessThan<NonComparable, std::optional<NonComparable>>);
-static_assert(!HasOperatorLessThan<NonComparable, std::optional<ThreeWayComparable>>);
-static_assert(!HasOperatorLessThan<ThreeWayComparable, std::optional<NonComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorLessThan<NonComparable, std::optional<NonComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorLessThan<NonComparable, std::optional<ThreeWayComparable>>);
+STATIC_ASSERT_OPTIONAL_CMP(!HasOperatorLessThan<ThreeWayComparable, std::optional<NonComparable>>);
 
 // LWG4072: avoid ambiguity with optional's own comparison operators
-static_assert(try_cmp_lt(std::optional<int>{}, std::optional<tester>{}) == std::optional<bool>{});
-static_assert(try_cmp_lt(std::optional<int>{}, std::optional<int>{}) == std::optional<bool>{false});
-
-#endif
+STATIC_ASSERT_OPTIONAL_CMP(try_cmp_lt(std::optional<int>{}, std::optional<tester>{}) == std::optional<bool>{});
+STATIC_ASSERT_OPTIONAL_CMP(try_cmp_lt(std::optional<int>{}, std::optional<int>{}) == std::optional<bool>{false});
 
 using std::optional;
 
diff --git a/libcxx/test/std/utilities/optional/optional.comp_with_t/not_equal.pass.cpp b/libcxx/test/std/utilities/optional/optional.comp_with_t/not_equal.pass.cpp
index e7940b9b421a3..8c9e6e2d73e9f 100644
--- a/libcxx/test/std/utilities/optional/optional.comp_with_t/not_equal.pass.cpp
+++ b/libcxx/test/std/utilities/optional/optional.comp_with_t/not_equal.pass.cpp
@@ -6,18 +6,23 @@
 //
 //===----------------------------------------------------------------------===//
 
-// UNSUPPORTED: c++03, c++11, c++14
+// REQUIRES: std-at-least-c++17
 // <optional>
 
 // template <class T, class U> constexpr bool operator!=(const optional<T>& x, const U& v);
 // template <class T, class U> constexpr bool operator!=(const U& v, const optional<T>& x);
 
 #include <optional>
+#include <type_traits>
 
 #include "test_comparisons.h"
 #include "test_macros.h"
 
 #if TEST_STD_VER >= 26
+#  define STATIC_ASSERT_OPTIONAL_CMP static_assert
+#else
+#  define STATIC_ASSERT_OPTIONAL_CMP LIBCPP_STATIC_ASSERT
+#endif
 
 struct tester {};
 
@@ -32,7 +37,7 @@ constexpr bool operator!=(const T& t, const tester& a) {
 
 template <class T, class U>
 constexpr std::optional<bool> try_cmp_neq(const T& t, const U& u) {
-  if constexpr (requires { t != u; })
+  if constexpr (HasOperatorNotEqual<const T, const U>)
     return t != u;
   else
     return {};
@@ -40,25 +45,23 @@ constexpr std::optional<bool> try_cmp_neq(const T& t, const U& u) {
 
 // Test SFINAE.
 
-static_assert(...
[truncated]

``````````

</details>


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


More information about the libcxx-commits mailing list