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 comments

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]

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.

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.