diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2024-06-18 13:27:02 +0100 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2024-06-21 10:10:10 +0100 |
commit | 510ce5eed69ee1bea9c2c696fe3b2301e16d1486 (patch) | |
tree | 9f2827ed2836e773ad7a56ed9dfc14ee2ce2a565 /libstdc++-v3/include/std/array | |
parent | e2fb245b07f489ed5bfd9a945e0053b4a3211245 (diff) | |
download | gcc-510ce5eed69ee1bea9c2c696fe3b2301e16d1486.zip gcc-510ce5eed69ee1bea9c2c696fe3b2301e16d1486.tar.gz gcc-510ce5eed69ee1bea9c2c696fe3b2301e16d1486.tar.bz2 |
libstdc++: Fix std::to_array for trivial-ish types [PR115522]
Due to PR c++/85723 the std::is_trivial trait is true for types with a
deleted default constructor, so the use of std::is_trivial in
std::to_array is not sufficient to ensure the type can be trivially
default constructed then filled using memcpy.
I also forgot that a type with a deleted assignment operator can still
be trivial, so we also need to check that it's assignable because the
is_constant_evaluated() path can't use memcpy.
Replace the uses of std::is_trivial with std::is_trivially_copyable
(needed for memcpy), std::is_trivially_default_constructible (needed so
that the default construction is valid and does no work) and
std::is_copy_assignable (needed for the constant evaluation case).
libstdc++-v3/ChangeLog:
PR libstdc++/115522
* include/std/array (to_array): Workaround the fact that
std::is_trivial is not sufficient to check that a type is
trivially default constructible and assignable.
* testsuite/23_containers/array/creation/115522.cc: New test.
Diffstat (limited to 'libstdc++-v3/include/std/array')
-rw-r--r-- | libstdc++-v3/include/std/array | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/libstdc++-v3/include/std/array b/libstdc++-v3/include/std/array index 3969547..8710bf7 100644 --- a/libstdc++-v3/include/std/array +++ b/libstdc++-v3/include/std/array @@ -431,7 +431,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_assert(is_constructible_v<_Tp, _Tp&>); if constexpr (is_constructible_v<_Tp, _Tp&>) { - if constexpr (is_trivial_v<_Tp>) + if constexpr (is_trivially_copyable_v<_Tp> + && is_trivially_default_constructible_v<_Tp> + && is_copy_assignable_v<_Tp>) { array<remove_cv_t<_Tp>, _Nm> __arr; if (!__is_constant_evaluated() && _Nm != 0) @@ -460,7 +462,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_assert(is_move_constructible_v<_Tp>); if constexpr (is_move_constructible_v<_Tp>) { - if constexpr (is_trivial_v<_Tp>) + if constexpr (is_trivially_copyable_v<_Tp> + && is_trivially_default_constructible_v<_Tp> + && is_copy_assignable_v<_Tp>) { array<remove_cv_t<_Tp>, _Nm> __arr; if (!__is_constant_evaluated() && _Nm != 0) |