Date: Tue, 8 Jul 2025 17:23:23 +0200
I believe there is an additional problem with your code - none of the
major C++ implementations have implemented starts_lifetime_as:
<https://en.cppreference.com/w/cpp/compiler_support/23.html>
Some testing with <https://godbolt.org> confirms that for gcc and clang.
So unless you have a toolchain that /does/ implement it, or you are
happy to implement it yourself as a temporary measure, you are
unfortunately out of luck even though the code is correct C++23.
<https://stackoverflow.com/questions/76445860/implementation-of-stdstart-lifetime-as>
David
On 08/07/2025 16:35, Yongwei Wu via Std-Discussion wrote:
> Thanks for the confirmation. I guessed so but was not very sure.
>
> On Tue, 8 Jul 2025 at 22:20, Brian Bi <bbi5291_at_[hidden]
> <mailto:bbi5291_at_[hidden]>> wrote:
>
> On Tue, Jul 8, 2025 at 9:57 AM Yongwei Wu via Std-Discussion
> <std-discussion_at_[hidden]
> <mailto:std-discussion_at_[hidden]>> 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
> <https://eel.is/c++draft/obj.lifetime#2>
>
>
> 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
> <https://eel.is/c++draft/basic.life#2.5>
>
>
major C++ implementations have implemented starts_lifetime_as:
<https://en.cppreference.com/w/cpp/compiler_support/23.html>
Some testing with <https://godbolt.org> confirms that for gcc and clang.
So unless you have a toolchain that /does/ implement it, or you are
happy to implement it yourself as a temporary measure, you are
unfortunately out of luck even though the code is correct C++23.
<https://stackoverflow.com/questions/76445860/implementation-of-stdstart-lifetime-as>
David
On 08/07/2025 16:35, Yongwei Wu via Std-Discussion wrote:
> Thanks for the confirmation. I guessed so but was not very sure.
>
> On Tue, 8 Jul 2025 at 22:20, Brian Bi <bbi5291_at_[hidden]
> <mailto:bbi5291_at_[hidden]>> wrote:
>
> On Tue, Jul 8, 2025 at 9:57 AM Yongwei Wu via Std-Discussion
> <std-discussion_at_[hidden]
> <mailto:std-discussion_at_[hidden]>> 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
> <https://eel.is/c++draft/obj.lifetime#2>
>
>
> 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
> <https://eel.is/c++draft/basic.life#2.5>
>
>
Received on 2025-07-08 15:23:31