See <https://wg21.link/p2590>.
1.1 can be done without any code but 1.2 makes std::start_lifetime_at <T> (p) retain the memory bit representation as initial value of the object? The paper mentions std::launder (ick), but I'm not sure how this is implementable at all if not sufficiently restricted.
See for example PR101641 where even existing cases are currently impossible to get right. So the only way to make std::start_lifetime_at <T> is to somehow emit a hard memory barrier for the compiler _and_ make sure the underlying storage is address-taken. Note this barrier has to prevail in RTL as well (I think std::launder is broken in this regard).
.
Do we really need to do anything at all (except defining the new stuff in a header), or possibly using __builtin_launder in it? I mean, my understanding of the start lifetime as something is as if with -flifetime-dse=1 there was a call to a constructor which just did nothing at all (like e.g. the struct X { int a, b; }; constructor does). And when we inline such an constructor, there is no CLOBBER and no code at all, so in the end nothing. __builtin_launder is just an optimization barrier (and indeed it can look like the address escapes). I think PR101641 is just bad example, we do allow type punning through union, but only if we can see it being done. The testcase is trying to change active union member through just a store through a pointer to union member, that is not what we support I believe.
https://gcc.gnu.org/pipermail/gcc-patches/2025-August/693665.html
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>: https://gcc.gnu.org/g:c1e1691b95e65c53a2b9c25de70c56655215b905 commit r16-3944-gc1e1691b95e65c53a2b9c25de70c56655215b905 Author: Jakub Jelinek <jakub@redhat.com> Date: Thu Sep 18 07:44:54 2025 +0200 libstdc++: Implement C++23 P2590R2 - Explicit lifetime management [PR106658] As I can't think of how the middle-end would treat __builtin_start_lifetime_as other than a blackbox and probably would need to be implemented as such inline asm in RTL, this patch just implements it using inline asm in the library. If not anything else, it can serve as fallback before we and/or clang get some builtin for it. Right now the inline asms pretend (potential) read from and write to the whole memory region and make optimizers forget where the return value points to. If the optimizers don't know where it points to, I think that should be good enough, but I'm a little bit afraid of possibly future optimizations trying to optimize q->c = 1; q->d = 2; auto p = std::start_lifetime_as<S>(q); if (p == reinterpret_cast<decltype (p)>(q)) return p->a + p->b; that because of the guarding condition or perhaps assertion we could simply use the q pointer in MEM_REFs with S type and be surprised by TBAA. Though if it is a must-alias case, then we should be fine as well. Though guess that would be the same case with a builtin. 2025-09-18 Jakub Jelinek <jakub@redhat.com> PR c++/106658 * include/bits/version.def: Implement C++23 P2590R2 - Explicit lifetime management. (start_lifetime_as): New. * include/bits/version.h: Regenerate. * include/std/memory (std::start_lifetime_as, std::start_lifetime_as_array): New function templates. * src/c++23/std.cc.in (std::start_lifetime_as, std::start_lifetime_as_array): Export. * testsuite/std/memory/start_lifetime_as/start_lifetime_as.cc: New test.
Implemented for GCC 16.
The master branch has been updated by Tomasz Kaminski <tkaminsk@gcc.gnu.org>: https://gcc.gnu.org/g:f8681ec8d0a778c62cf6d921e00a8714529ecca8 commit r16-4023-gf8681ec8d0a778c62cf6d921e00a8714529ecca8 Author: Tomasz KamiÅski <tkaminsk@redhat.com> Date: Mon Sep 22 13:31:17 2025 +0200 libstdc++: Move start_lifetime_as functions to bits/stl_construct.h [PR106658] This allows inplace_vector to use these functions without including the entire <memory> header. Preprocessor checks are changed to use __glibcxx macros, so new functions are available outside memory header, that exports __cpp_lib macros. PR libstdc++/106658 libstdc++-v3/ChangeLog: * include/bits/stl_construct.h (std::start_lifetime_as_array) (std::start_lifetime_as): Moved from std/memory, with update to guard macros. * include/std/memory (std::start_lifetime_as_array) (std::start_lifetime_as): Moved to bits/stl_construct.h. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz KamiÅski <tkaminsk@redhat.com>