libstdc++: Use std::construct_at in std::common_iterator [PR103992]
authorJonathan Wakely <jwakely@redhat.com>
Wed, 12 Jan 2022 16:58:18 +0000 (16:58 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 14 Jan 2022 10:14:24 +0000 (10:14 +0000)
This should have been done as part of the LWG 3574 changes.

libstdc++-v3/ChangeLog:

PR libstdc++/103992
* include/bits/stl_iterator.h (common_iterator): Use
std::construct_at instead of placement new.
* testsuite/24_iterators/common_iterator/1.cc: Check copy
construction is usable in constant expressions.

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

index 24a71ea..24c106e 100644 (file)
@@ -1907,14 +1907,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
            if constexpr (is_trivially_default_constructible_v<_It>)
              _M_it = std::move(__x._M_it);
            else
-             ::new((void*)std::__addressof(_M_it)) _It(__x._M_it);
+             std::construct_at(std::__addressof(_M_it), __x._M_it);
          }
        else if (_M_index == 1)
          {
            if constexpr (is_trivially_default_constructible_v<_Sent>)
              _M_sent = std::move(__x._M_sent);
            else
-             ::new((void*)std::__addressof(_M_sent)) _Sent(__x._M_sent);
+             std::construct_at(std::__addressof(_M_sent), __x._M_sent);
          }
       }
 
@@ -1928,14 +1928,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          if constexpr (is_trivially_default_constructible_v<_It>)
            _M_it = std::move(__x._M_it);
          else
-           ::new((void*)std::__addressof(_M_it)) _It(__x._M_it);
+           std::construct_at(std::__addressof(_M_it), __x._M_it);
        }
       else if (_M_index == 1)
        {
          if constexpr (is_trivially_default_constructible_v<_Sent>)
            _M_sent = std::move(__x._M_sent);
          else
-           ::new((void*)std::__addressof(_M_sent)) _Sent(__x._M_sent);
+           std::construct_at(std::__addressof(_M_sent), __x._M_sent);
        }
     }
 
index 484d0cc..365ee89 100644 (file)
@@ -157,6 +157,22 @@ test04()
   VERIFY( x.i == 2 );
 }
 
+constexpr bool
+test_pr103992()
+{
+  using C1 = std::common_iterator<std::reverse_iterator<int*>,
+                                 std::unreachable_sentinel_t>;
+  using C2 = std::common_iterator<std::reverse_iterator<const int*>,
+                                 std::unreachable_sentinel_t>;
+  C1 c1;
+  C2 c2 = c1;
+  C1 c3 = c1;
+
+  return true;
+}
+
+static_assert( test_pr103992() );
+
 int
 main()
 {