r/cpp • u/SkoomaDentist Antimodern C++, Embedded, Audio • Aug 05 '25
Why still no start_lifetime_as?
C++ has desperately needed a standard UB-free way to tell the compiler that "*ptr is from this moment on valid data of type X, deal with it" for decades. C++23 start_lifetime_as promises to do exactly that except apparently no compiler supports it even two years after C++23 was finalized. What's going on here? Why is it apparently so low priority? Surely it can't be a massive undertaking like modules (which require build system coordination and all that)?
107
Upvotes
57
u/kitsnet Aug 05 '25
I think it's because any sane compiler already avoids doing optimization that
start_lifetime_aswould disable.8
u/sheckey Aug 05 '25
Is this feature meant to be a more precise way of stating intent so that the desired outcome is still achieved under heavier amounts of optimization? I saw a nice article that described the difference between using this simply and using reinterpet_cast for pod types over some raw bytes. Is the feature clarifying the intent so that the optimizer won‘t do something unwanted, or is it just shoring up the situation for good measure, or? thank you!
6
u/cristi1990an ++ Aug 05 '25
Probably because it requires compiler support. It's not purely a library feature. Compiler features are always adopted slower by vendors depending on their complexity and importance. start_lifetime_as is niche, no library feature depends on it and most probably it's not exactly trivial to implement. Concepts and constexpr extensions for example were also big compiler features, but since C++20 most library features had dependencies on them, so I assume they were prioritized.
6
u/pavel_v Aug 06 '25
We are using the below implementation which is "stolen" from this talk
3
u/13steinj Aug 05 '25
You can implement it yourself with a (static? Or reinterpret, I forget) cast + a memmove from the buffer to itself, which should get optimized out.
1
u/sweetno Aug 05 '25
Is it UB-free though? Cppreference directly lists unaligned access as UB for this one.
-2
u/pjmlp Aug 05 '25
Probably because many standard features are not existing practice, rather invented at WG21, and eventually compiler vendors are supposed to implement them.
Maybe we should start tracking down which mailing papers are coming from people doing commits to compilers, or their standard libraries, and which aren't.
0
u/[deleted] Aug 05 '25
[deleted] — view deleted comment
0
u/[deleted] Aug 06 '25
[deleted] — view deleted comment