Last Updated: February 25, 2016
·
533
· kazark

std::vector is not always what it seems

Public service announcement to all you young, naive C++ programmers out there like me! Your trusty std::vector<...> is not always what it seems! std::vector<bool> is a lie. Don't use it unless your C++-fu is great enough that you already knew what I was talking about before you began reading this post, in which case you already know enough not to use it.

I spent a couple hours this morning banging my head off my desk because of the apparently eradic and inexplicable behavior of this nonconformant container. Had I only noticed that std::vector<bool> is actually its own specialization of std::vector! It is a micro-optimized version of std::vector which breaks some of the expected behavior. It throws away the continous memory guarantee. If you don't believe me, just ask Herb Sutter. See also Alternative to vector<bool> on StackOverflow.

Beware std::vector<bool>! And I hope that, unlike me, you find out before banging your head off your desk.

1 Response
Add your response

Hehehe i Like your Pro Tip to not use it :D I will write a pro Tip about that probally too. But will advice to code with C wrappers and not nativ C if you don't know such stuff :D. Thats one of the Points where JavaScript becomes Handy :D

over 1 year ago ·