[Bug libstdc++/120397] std::uninitialized_value_construct cannot create arrays of non-trivially destructible types

2025-06-13 Thread redi at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120397

Jonathan Wakely  changed:

   What|Removed |Added

 Status|NEW |RESOLVED
   Target Milestone|--- |16.0
 Resolution|--- |FIXED

--- Comment #6 from Jonathan Wakely  ---
Fixed on trunk. I doubt anybody cares about this, so not worth backporting.

[Bug libstdc++/120397] std::uninitialized_value_construct cannot create arrays of non-trivially destructible types

2025-06-13 Thread cvs-commit at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120397

--- Comment #5 from GCC Commits  ---
The master branch has been updated by Jonathan Wakely :

https://gcc.gnu.org/g:3af71d4ac80751afc2f0ef7efdeb07dc18155689

commit r16-1504-g3af71d4ac80751afc2f0ef7efdeb07dc18155689
Author: Jonathan Wakely 
Date:   Thu May 22 15:42:45 2025 +0100

libstdc++: Fix std::uninitialized_value_construct for arrays [PR120397]

The std::uninitialized_{value,default}_construct{,_n} algorithms should
be able to create arrays, but that currently fails because when an
exception happens they clean up using std::_Destroy and in C++17 that
doesn't support destroying arrays. (For C++20 and later, std::destroy
does handle destroying arrays.)

This commit adjusts the _UninitDestroyGuard RAII type used by those
algos so that in C++17 mode it recursively destroys each rank of an
array type, only using std::_Destroy for the last rank when it's
destroying non-array objects.

libstdc++-v3/ChangeLog:

PR libstdc++/120397
* include/bits/stl_uninitialized.h (_UninitDestroyGuard):
Add new member function _S_destroy and call it from the
destructor (for C++17 only).
*
testsuite/20_util/specialized_algorithms/uninitialized_default_construct/120397.cc:
New test.
*
testsuite/20_util/specialized_algorithms/uninitialized_value_construct/120397.cc:
New test.

Reviewed-by: Tomasz KamiÅski 

[Bug libstdc++/120397] std::uninitialized_value_construct cannot create arrays of non-trivially destructible types

2025-05-22 Thread redi at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120397

--- Comment #4 from Jonathan Wakely  ---
--- a/libstdc++-v3/include/bits/stl_uninitialized.h
+++ b/libstdc++-v3/include/bits/stl_uninitialized.h
@@ -118,7 +118,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   ~_UninitDestroyGuard()
   {
if (__builtin_expect(_M_cur != 0, 0))
- std::_Destroy(_M_first, *_M_cur);
+ _S_destroy(_M_first, *_M_cur);
   }

   _GLIBCXX20_CONSTEXPR
@@ -129,6 +129,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION

 private:
   _UninitDestroyGuard(const _UninitDestroyGuard&);
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wc++17-extensions"
+  template
+   _GLIBCXX20_CONSTEXPR
+   static void
+   _S_destroy(_Iter __first, _Iter __last)
+   {
+#if __cplusplus == 201703L
+ using _ValT = typename iterator_traits<_Iter>::value_type;
+ if constexpr (is_array<_ValT>::value)
+   for (; __first != __last; ++__first)
+ _S_destroy(*__first, *__first + extent<_ValT>::value);
+ else
+#endif
+ std::_Destroy(__first, __last);
+   }
+#pragma GCC diagnostic push
 };

   // This is the default implementation of std::uninitialized_copy.

[Bug libstdc++/120397] std::uninitialized_value_construct cannot create arrays of non-trivially destructible types

2025-05-22 Thread redi at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120397

--- Comment #3 from Jonathan Wakely  ---
  ~_UninitDestroyGuard()
  {
if (__builtin_expect(_M_cur != 0, 0))
  std::_Destroy(_M_first, *_M_cur);
  }

If we handle arrays here then it doesn't impact other uses of std::_Destroy

[Bug libstdc++/120397] std::uninitialized_value_construct cannot create arrays of non-trivially destructible types

2025-05-22 Thread redi at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120397

--- Comment #2 from Jonathan Wakely  ---
(In reply to Jonathan Wakely from comment #0)
> We could either stop using std::_Destroy(first, cur) in the uninitialized
> algos (no thanks), or make std::_Destroy(first, cur) handle arrays, or make
> std::_Destroy(*first) handle arrays.
> 
> The latter would be consistent with std::destroy_at, and would mean that
> std::_Destroy_n(first, n) would also work for arrays. But to make the code
> above (and all similar cases?) work it's sufficient to handle it in
> std::_Destroy(first, last). All the uninitialized algos use that.

Hmm, but std::destroy(first, last) and std::destroy_n(first, n) in C++17 do not
support arrays, and we implement them by calling std::_Destroy(first, last) and
std::_Destroy_n(first, n) respectively. So if _Destroy is "fixed" to handle
arrays, then it would affect std::destroy.

[Bug libstdc++/120397] std::uninitialized_value_construct cannot create arrays of non-trivially destructible types

2025-05-22 Thread redi at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120397

Jonathan Wakely  changed:

   What|Removed |Added

   Last reconfirmed||2025-05-22
 Ever confirmed|0   |1
 Status|UNCONFIRMED |NEW

--- Comment #1 from Jonathan Wakely  ---
When the type has a non-trivial default constructor then uninitialized_default
has the same problem:

#include 

struct X { X() { } ~X() { } };

void val(X (*x)[1])
{
  std::uninitialized_value_construct(x, x+1);
}
void val_n(X (*x)[1])
{
  std::uninitialized_value_construct_n(x, 1);
}
void def(X (*x)[1])
{
  std::uninitialized_default_construct(x, x+1);
}
void def_n(X (*x)[1])
{
  std::uninitialized_default_construct_n(x, 1);
}

I don't think any of the other std::uninitialized_xxx algos in C++17 can be
used to create arrays, so it's only these ones.