r/cpp • • Aug 26 '26

C++26: std::inplace_vector

https://www.sandordargo.com/blog/2026/08/26/cpp26-inplace-vector
181 Upvotes

121 comments sorted by

View all comments

Show parent comment

2

u/n1ghtyunso Aug 28 '26

Apparently I misread your last sentence so I got really confused where you think pointers are involved here and how that is related to zero capacity, but you are talking about ways to avoid the memory cost of an explicit size member for N > 0, I see.
Sorry for the confusion.

I don't think any implementation will go out of its way to specifically do this, unless a more general tombstone or padding-discovery facility was made available to identify a set of usable bit patterns in a type to side-car the current size value.
I guess technically for sizeof(T) >= 8, you really only need one single padding bit inside T to store if its at capacity or below, and if its below, you can use the full last slot to represent the actual size (maybe need to shuffle the bits abit ugh).
I don't even want to start thinking about how to make this formally C++ spec-compliant.

1

u/KuntaStillSingle Aug 28 '26

Yeah I think the only obvious case would be using a smaller type than size_t if N can be represented and alignof(T) is less than alignof(size_t), for example <char, 15> could usually be 16 bytes with a uint8_t size member rather than usually 24 for size_t member + padding.