libstdc++: Fix std::common_iterator triviality [PR100823]
authorJonathan Wakely <jwakely@redhat.com>
Wed, 20 Jul 2022 15:51:44 +0000 (16:51 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 20 Jul 2022 22:38:48 +0000 (23:38 +0100)
This fixes the remaining problem reported in the PR, that the special
members should be trivial.  This can be done by constraining the
non-trivial versions and adding defaulted overloads that will be used
when the union members are trivial.

Making these members trivial alters the argument passing ABI and so
isn't suitable for backporting to release branches.

libstdc++-v3/ChangeLog:

PR libstdc++/100823
* include/bits/stl_iterator.h (common_iterator): Define
destructor, copy constructor and move constructor as trivial
when the underlying types allow.
* testsuite/24_iterators/common_iterator/100823.cc: Check
triviality of special members.

libstdc++-v3/include/bits/stl_iterator.h
libstdc++-v3/testsuite/24_iterators/common_iterator/100823.cc

index a913c04..9cd262c 100644 (file)
@@ -1925,9 +1925,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          }
       }
 
+    common_iterator(const common_iterator&) = default;
+
     constexpr
     common_iterator(const common_iterator& __x)
     noexcept(_S_noexcept<const _It&, const _Sent&>())
+    requires (!is_trivially_copyable_v<_It> || !is_trivially_copyable_v<_Sent>)
     : _M_valueless(), _M_index(__x._M_index)
     {
       if (_M_index == 0)
@@ -1946,9 +1949,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        }
     }
 
+    common_iterator(common_iterator&&) = default;
+
     constexpr
     common_iterator(common_iterator&& __x)
     noexcept(_S_noexcept<_It, _Sent>())
+    requires (!is_trivially_copyable_v<_It> || !is_trivially_copyable_v<_Sent>)
     : _M_valueless(), _M_index(__x._M_index)
     {
       if (_M_index == 0)
@@ -2017,8 +2023,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        return *this;
       }
 
+#if __cpp_concepts >= 202002L // Constrained special member functions
+    ~common_iterator() = default;
+
     constexpr
     ~common_iterator()
+      requires (!is_trivially_destructible_v<_It>
+                 || !is_trivially_destructible_v<_Sent>)
+#else
+    constexpr
+    ~common_iterator()
+#endif
     {
       if (_M_index == 0)
        _M_it.~_It();
index 4f2b23d..b42dd08 100644 (file)
@@ -5,6 +5,21 @@
 #include <testsuite_hooks.h>
 
 void
+test_triviality()
+{
+  using I = std::common_iterator<int*, const int*>;
+
+  // Cannot be trivial, because it has to initialize members.
+  static_assert( ! std::is_trivially_default_constructible_v<I> );
+
+  static_assert( std::is_trivially_destructible_v<I> );
+  static_assert( std::is_trivially_copy_constructible_v<I> );
+  static_assert( std::is_trivially_copy_assignable_v<I> );
+  static_assert( std::is_trivially_move_constructible_v<I> );
+  static_assert( std::is_trivially_move_assignable_v<I> );
+}
+
+void
 test_valueless_assignment()
 {
   int x[1] { };