]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Implement const copy-assignment for tuple<> [PR119721]
authorTomasz Kamiński <tkaminsk@redhat.com>
Mon, 27 Oct 2025 13:19:47 +0000 (14:19 +0100)
committerTomasz Kamiński <tkaminsk@redhat.com>
Wed, 29 Oct 2025 16:13:59 +0000 (17:13 +0100)
This patch completes the implementation of P2321R2, giving tuple proper proxy
reference semantics.

The assignment operator is implemented as a template constrained to accept only
tuple<>. Consequently, the language does not consider it a copy assignment
operator, which prevents tuple<> from losing its trivially copyable status.

The _Tuple template parameter is defaulted, ensuring the operator remains
a viable candidate for assignment with an empty brace-init list.

PR libstdc++/119721

libstdc++-v3/ChangeLog:

* include/std/tuple (tuple<>::operator=(const _Tuple&) const)
[__cpp_lib_ranges_zip]: Define.
* testsuite/23_containers/tuple/cons/119721.cc: Test const
assignment.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
libstdc++-v3/include/std/tuple
libstdc++-v3/testsuite/23_containers/tuple/cons/119721.cc

index 6edcf5e55536b7ec8bac570ea899125f18058b09..d4db12557ab632e06283232856a8acb6e15eb692 100644 (file)
@@ -1996,6 +1996,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       void swap(tuple&) noexcept { /* no-op */ }
 
 #if __cpp_lib_ranges_zip // >= C++23
+      template<same_as<tuple> _Tuple = tuple>
+      constexpr const tuple&
+      operator=(const _Tuple&) const noexcept
+      { return *this; }
+
       constexpr void swap(const tuple&) const noexcept
       { /* no-op */ }
 #endif
index 240f1a5e8d7872ab7a8eb63f71c11153244328a4..1d152386fb7b4a8976190c86e17a18623f24395f 100644 (file)
@@ -69,12 +69,22 @@ test04()
 {
   std::array<int, 0> a{};
   const std::tuple<> t1;
-  
+
   // Const assignment from array
-  t1 = a;
-  t1 = std::move(a);
-  
-  VERIFY( t1 == a );
+  std::tuple<> t2;
+  const std::tuple<>& r1 = (t1 = t2);
+  VERIFY( &r1 == &t1 );
+  const std::tuple<>& r2 = (t1 = std::move(t2));
+  VERIFY( &r2 == &t1 );
+
+  const std::tuple<>& r3 = (t1 = {});
+  VERIFY( &r3 == &t1 );
+
+  // Const assignment from array
+  const std::tuple<>& r4 = (t1 = a);
+  VERIFY( &r4 == &t1 );
+  const std::tuple<>& r5 = (t1 = std::move(a));
+  VERIFY( &r5 == &t1 );
 }
 
 void