https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95349
--- Comment #50 from rguenther at suse dot de <rguenther at suse dot de> --- On Mon, 3 Jun 2024, Christopher.Nerz at de dot bosch.com wrote: > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95349 > > --- Comment #49 from Christopher Nerz <Christopher.Nerz at de dot bosch.com> > --- > Ah, misunderstood and therefore forgot optimization to a constant. > > In the current code example, we have the problem that the (second) > initialization does not initialize with a value (ergo undefined behaviour due > to uninitialized variable although it is created in an initialized memory). > In the more complex code - which I am trying to simplify, but so far get the > behaviour only in quite special situations which are hard to reduce to an > example - we have that the buffer is explicitly initialized via `new T{}` > (calling an inline initialization, i.e. not a trivial default constructor, but > T is trivially copy & move constructible), then the buffer is copied (as > std::byte-array) and then std::launder is applied to the resulting byte-array. > To my understanding [basic.types.general].$3 > > > For two distinct objects obj1 and obj2 of trivially copyable type T, where > > neither obj1 nor obj2 is a potentially-overlapping subobject, if the > > underlying bytes (6.7.1) making up obj1 are copied into obj2, obj2 shall > > subsequently hold the same value as obj1. > > guarantees that the copied buffer can be used as `T` via > `*std::launder<T*>(second_buffer)`. Clang, MSVC agree, gcc does not. > > Again: Still trying to construct the minimal example for that behavior :-/ Hmm, copying as std::byte-array should make the store use alias-set zero so a followup load as T should be OK. Unless "then the buffer is copied (as std::byte-array)" is really doing something more advanced with regard to type-based aliasing rules (thus not a plain memcpy).