r/cpp • u/User_Deprecated • Aug 26 '26
C++26: std::inplace_vector
https://www.sandordargo.com/blog/2026/08/26/cpp26-inplace-vector
181
Upvotes
r/cpp • u/User_Deprecated • Aug 26 '26
View all comments
Show parent comment
10
u/BarryRevzin Aug 27 '26
So what? They're all pretty straightforward, simpler even than in
vectorbecause we know there's no allocation. It doesn't take long to implement. Even less so these days.That might be more general, but it's also worse, which is why nobody does this. David Stone had a pair of good CppCon talks about vectors in general, I think he covers it in this one (or, if not that, this one). The problem with
vector<T, inplace_allocator<T, N>>is that it's less efficient (and requires more space!) thaninplace_vector<T, N>. In order for it to not suck,vectorwould basically have to customize its own storage for that case, which now requires more work than just having writteninplace_vector<T, N>to begin with. This goes double forstd::string, whose storage is more complicated.On the other hand, an
inplace_string<N>is useful, is distinct fromstd::string, and we implement ours in terms of ourinplace_vector<char, N>(orN+1depending on whether we want to enforce null termination).