On Tue, Jul 8, 2025 at 5:02 PM Tom Honermann <tom@honermann.net> wrote:
On 7/8/25 10:20 AM, Brian Bi via Std-Discussion wrote:
On Tue, Jul 8, 2025 at 9:57 AM Yongwei Wu via Std-Discussion <std-discussion@lists.isocpp.org> wrote:
Assume I am working on a little-endian machine only. Is the following
code undefined behaviour?

uint32_t data = 0x40400000;
auto f = *std::start_lifetime_as<float>(&data);

It's fine as long as `sizeof(float) <= sizeof(uint32_t)`. See https://eel.is/c++draft/obj.lifetime#2

Is it not also the case that implicit object creation requires that the storage be provided by an object that meets the provides storage requirements? https://eel.is/c++draft/intro.object#3. I thought it was required, but I'm unable to find clear wording now.

No, the old object doesn't have to provide storage for the new object.

But because it doesn't (in this case), the new object is not "nested within" the old object, triggering [basic.life]/2.5, ending the lifetime of the old object.

In general, C++ allows you to overwrite an object of arbitrary type (with some restrictions) with an object of some other arbitrary type. You've always been able to do that with placement new, and now you can do it with `std::start_lifetime_as` too. But you have to deal with the consequences of it (e.g., the UB of the original implicit destructor call).

 

If it is not, is the data variable still accessible now?

No. The `data` object's lifetime has ended because its storage has been reused. https://eel.is/c++draft/basic.life#2.5

And the old object is not one that is transparently replaceable by the new object. https://eel.is/c++draft/basic.life#10

Tom.

 

Best regards,

Yongwei

--
Yongwei Wu
URL: http://wyw.dcweb.cn/
--
Std-Discussion mailing list
Std-Discussion@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion


--
Brian Bi



--
Brian Bi