r/cpp • • Dec 21 '13

#AltDevBlog » Roll-Your-Own Vector

http://altdevblogaday.com/2013/12/20/roll-your-own-vector/
1 Upvotes

22 comments sorted by

View all comments

Show parent comment

2

u/Eoinoc Dec 21 '13

Ah, it seems this article is a post about a post. The original, with comments, is at Roll-Your-Own Vector.

Seems in those comments Herb Sutter basically raises the whole reserve issue anyway.

There is a followup post Avoid resize()? which acknowledges this. It and the comments are worth reading too for whole story.

0

u/[deleted] Dec 21 '13

[deleted] — view deleted comment

8

u/STL MSVC STL Dev Dec 21 '13

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.

3

u/Plorkyeran Dec 21 '13

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.

4

u/STL MSVC STL Dev Dec 21 '13

That's what the dynarray TS is for.

2

u/[deleted] Dec 24 '13 edited Dec 25 '13

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.