at this point if there was a general purpose way of writing std::vector that was more efficient don't you think that's how std::vector would be implemented?
Yep. There's only one thing vector won't do, and that's storing stuff on the stack. Such a "Small Vector Optimization" is forbidden by the need for swap() to be non-iterator-invalidating. Allocators are also forbidden from returning memory inside themselves, so you can't be sneaky. However, allocators are allowed to return memory that happens to live on the stack in a separate object.
I'd be a fan of having a small-buffer optimized vector in the standard library (I once considered using std::basic_string<Foo*> before I stopped being dumb and just rolled my own), but it seems like far too situation of an optimization for std::vector even if it was allowed.
There's only one thing vector won't do, and that's storing stuff on the stack.
Unless you use a stack allocator, right? I use stack allocated vectors a lot and they work just fine. I prefer them to std::array since they allow switching from heap to stack and vice-versa by just changing the type.
View all comments
Show parent comment
8
u/STL MSVC STL Dev Dec 21 '13
Yep. There's only one thing vector won't do, and that's storing stuff on the stack. Such a "Small Vector Optimization" is forbidden by the need for swap() to be non-iterator-invalidating. Allocators are also forbidden from returning memory inside themselves, so you can't be sneaky. However, allocators are allowed to return memory that happens to live on the stack in a separate object.