[libcxx-commits] [libcxx] [libc++] Make std::pair trivially copyable if its members are (PR #89652)

Nico Weber via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 23 11:31:11 PDT 2024


nico wrote:

https://isocpp.org/files/papers/N4860.pdf 20.4.2 Class template pair [pairs.pair]p23 (about the assignment op): "Remarks: This operator is defined as deleted unless is_copy_assignable_v<first_type> is true and
is_copy_assignable_v<second_type> is true."

p28 (on the move assignment op): "Constraints:
(28.1) — is_move_assignable_v<first_type> is true and
(28.2) — is_move_assignable_v<second_type> is true."

The new __has_defaulted_members doesn't honor those. If I make them honor it as below (only did the C++20 branch), my code stays working as-is:

```diff
% git diff
diff --git a/libcxx/include/__utility/pair.h b/libcxx/include/__utility/pair.h
index c0002b7abb3c..bc30eb6ca718 100644
--- a/libcxx/include/__utility/pair.h
+++ b/libcxx/include/__utility/pair.h
@@ -92,11 +92,21 @@ struct _LIBCPP_TEMPLATE_VIS pair
   };
 #  if _LIBCPP_STD_VER >= 20
   _LIBCPP_HIDE_FROM_ABI constexpr pair& operator=(const pair&)
-    requires __has_defaulted_members::value
+    requires (__has_defaulted_members::value &&
+              is_move_assignable<first_type>::value &&
+              is_move_assignable<second_type>::value)
   = default;
 
+  _LIBCPP_HIDE_FROM_ABI constexpr pair& operator=(const pair&)
+    requires (__has_defaulted_members::value &&
+              !(is_move_assignable<first_type>::value &&
+                is_move_assignable<second_type>::value))
+  = delete;
+
   _LIBCPP_HIDE_FROM_ABI constexpr pair& operator=(pair&&)
-    requires __has_defaulted_members::value
+    requires (__has_defaulted_members::value &&
+              is_move_assignable<first_type>::value &&
+              is_move_assignable<second_type>::value)
   = default;
 #  elif __has_attribute(__enable_if__)
   _LIBCPP_HIDE_FROM_ABI pair& operator=(const pair&)
```

Looks like the PR as-submitted might not be incomplete, maybe. (I'm not a standards expert though.)

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


More information about the libcxx-commits mailing list