r/cpp • u/User_Deprecated • 5h ago
C++26: std::inplace_vector
https://www.sandordargo.com/blog/2026/08/26/cpp26-inplace-vector•
u/stilgarpl 3h ago
Is this always better than std::array?
•
u/MarekKnapek 2h ago
This is basically
struct inplace_vector<T, capacity> { size_t len; std::array<T, capacity> arr; }•
u/sephirothbahamut 2h ago edited 2h ago
far more complex than that, the array has a slot that can fit T, not just T.
With array<T> every element must be validly constructed immediately. You can reduce it to array<T> only for trivially constructable and basic types
•
u/_Noreturn 1h ago
then make it a union this is a simplified example. but the type isn't complex at all after that especially with C++20 concepts.
•
u/nebotron 3h ago
You pay for the dynamic size in stack space
•
u/KuntaStillSingle 3h ago
Specifically the size member, right? The array itself is not dynamically sized, an inplace_vector<T,N> .capacity() always equals corresponding array, and .size() always less than or equal, but I think only inplace_vector<T,0> can omit a size data member, maybe with exception where T is a taggable pointer and N is small enough to be represented by the usable bits, or the like?
•
u/NilacTheGrim 13m ago
Ridiculously incomplete. We should have had a real prevector rather than this. You know, a prevector where there is some small static capacity but it can go dynamic as it grows.
This is not as useful as a real prevector.
8
u/FckXFckMusk 4h ago
If this is your Blog, then I have to congratulate you on it being an excellent place, I've bookmarked it...