r/cpp • 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)?

105 Upvotes

64 comments sorted by

View all comments

Show parent comment

1

u/SoerenNissen Aug 07 '25 edited Aug 07 '25

https://en.cppreference.com/w/cpp/language/pointer.html

... ok let me see if I can understand "int A::* p = 0;" correctly in the light of that.

It allows you to replace this:

auto p = offsetof(A,int_member);
A a = {7};
std::cout << *(int*)((ptrdiff_t)&a) + p);

With something substantially more type safe:

int A::* p = &A::int_member;
A a = {7};
std::cout << a.*p;

I understand it such that p is an integer pointer but not any abitrary integer pointer. If I set it, it must specifically point to an integer stored inside an A. Now, A doesn't have any int members but that's OK because it's a nullptr

However, if we could set it to something, it wouldn't actually point to "an int," - it contains only the offset down to that int, such that must supply the object along with the pointer to get a valid int.

And the reason it works with inheritance is that the type is specifically associated with A::, such that if I use it with a subclass of A, the additional offset (if any) is known by the compiler.

Does any of that sound off?

2

u/simonask_ Aug 07 '25

That matches my understanding. :-) Lots of caveats around offsets here, but yeah.

1

u/SoerenNissen Aug 07 '25

Yeah I get that - I use offsets so little, I hadn't even learned about pointer-to-member outside of pointer-to-member-function

(And even those, I use very little - effectively, every time I need to pass a member function somewhere, I'm in a situation where I can just wrap it all in a lambda pass that instead.)