[libcxx-commits] [PATCH] D136050: [libc++] Fix missing requires clause on variant operator<=>

Joe Loser via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sun Oct 16 19:26:34 PDT 2022


jloser created this revision.
jloser added reviewers: mumbleskates, ldionne, Mordante, philnik, CaseyCarter.
Herald added a project: All.
jloser requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

`std::variant::operator<=>` is missing a requires clause ensuring that
`operator<=>` only exists when all of the types in the variant are
`three_way_comparable`.

Add the missing requires clause and adjust the existing test which was
incorrect.

Fixes https://github.com/llvm/llvm-project/issues/58192.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136050

Files:
  libcxx/include/variant
  libcxx/test/std/utilities/variant/variant.relops/three_way.pass.cpp


Index: libcxx/test/std/utilities/variant/variant.relops/three_way.pass.cpp
===================================================================
--- libcxx/test/std/utilities/variant/variant.relops/three_way.pass.cpp
+++ libcxx/test/std/utilities/variant/variant.relops/three_way.pass.cpp
@@ -173,12 +173,8 @@
 static_assert( has_three_way_op<HasOnlySpaceship>);
 static_assert( has_three_way_op<std::variant<int, HasOnlySpaceship>>);
 
-// variants containing types with unavailable operator== still exist but will
-// generate a compilation error if their operator== is invoked, so the variant
-// type here participates when asked for operator== and operator<=> even though
-// it would actually fail.
 static_assert(!three_way_comparable<HasOnlySpaceship>);
-static_assert( three_way_comparable<std::variant<int, HasOnlySpaceship>>);
+static_assert(!three_way_comparable<std::variant<int, HasOnlySpaceship>>);
 
 static_assert( has_three_way_op<HasFullOrdering>);
 static_assert( has_three_way_op<std::variant<int, HasFullOrdering>>);
Index: libcxx/include/variant
===================================================================
--- libcxx/include/variant
+++ libcxx/include/variant
@@ -1638,7 +1638,7 @@
 
 #  if _LIBCPP_STD_VER > 17
 
-template <class... _Types>
+template <class... _Types> requires (three_way_comparable<_Types> && ...)
 _LIBCPP_HIDE_FROM_ABI constexpr common_comparison_category_t<compare_three_way_result_t<_Types>...>
 operator<=>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
   using __variant_detail::__visitation::__variant;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136050.468110.patch
Type: text/x-patch
Size: 1580 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20221017/f538f7e7/attachment.bin>


More information about the libcxx-commits mailing list