https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122907
--- Comment #7 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Jonathan Wakely <[email protected]>: https://gcc.gnu.org/g:7a5ad555965d103e73e606417f4289a4238e82d4 commit r16-6184-g7a5ad555965d103e73e606417f4289a4238e82d4 Author: Jonathan Wakely <[email protected]> Date: Tue Dec 2 15:26:31 2025 +0000 libstdc++: Do not optimize std::copy to memcpy for bool output [PR122907] Copying narrow characters to a range of bool using std::copy cannot be optimized to use std::memcpy. Assignment of an arbitrary integer to a bool needs to convert all non-zero values to true, so is not a simple memcpy-like or bit_cast-like operation. We currently get this wrong and optimize it to memcpy, producing invalid bool values. By making __memcpyable_integer<bool> false we disable memcpy optimizations for heterogeneous std::copy and std::move calls where either the source or destination type is bool. Copies where both types are bool can still optimize to memcpy, because we don't check __memcpyable_integer in that case. This disables the memcpy optimization for bool as the source type, which isn't actually necessary (the representation of bool in GCC is 0x00 or 0x01 and so copying bool to char is just a bit_cast). We don't currently have a straightforward way to allow memcpy for bool to char but disallow the inverse. This seems acceptable as using std::copy with bool inputs and narrow character outputs is probably not common enough for this to be an important optimization to do in the library code. libstdc++-v3/ChangeLog: PR libstdc++/122907 * include/bits/cpp_type_traits.h (__memcpyable_integer<bool>): Define as false. * testsuite/25_algorithms/copy/122907.cc: New test. Reviewed-by: Tomasz KamiÅski <[email protected]> Reviewed-by: Patrick Palka <[email protected]>
