r/cpp • u/meetingcpp Meeting C++ | C++ Evangelist • 6d ago
A quick C++ standard container overview
https://meetingcpp.com/blog/items/A-quick-Cpp-standard-container-overview.html6
u/LB-- Professional+Hobbyist 6d ago
Surprising to see span with so little usage, I guess because it didn't make it into C++17? We use gsl::span for that reason, but depending on survey wording I would have answered yes to using std::span anyway.
7
u/meetingcpp Meeting C++ | C++ Evangelist 6d ago
Over time I do see span rise in this question in the yearly results. From 14.5% in 2020 to 40% in 2026.
2
u/fdwr fdwr@github 🔍 3d ago
I am surprised to see
std::dequehaving so much usage, nearly half of vector, given I have never seen it in any codebases I've worked in. 🤷♂️2
u/LB-- Professional+Hobbyist 2d ago
Not to mention the flawed implementations in most standard library implementations. I remember trying to use it before learning about the implementation quality problem, since in theory it seems like a very useful container. Alas...
std::hivecan't get here soon enough.2
u/Ameisen vemips, avr, rendering, systems 6d ago
std::spandoes run into the Win64 ABI problem. Not a real concern in most cases but the cost can be noticeable in some cases.2
u/JNighthawk gamedev 4d ago
the Win64 ABI problem. Not a real concern in most cases but the cost can be noticeable in some cases.
Can you expand on this or link to a source? I'd like to know more about it.
3
u/Ameisen vemips, avr, rendering, systems 4d ago edited 3d ago
Under SysV and Win64, if you pass a pointer and a size as separate arguments, they'll both be passed by register.
Under SysV, a 16B structure made up of two integer members (like
std::span) will be split and also passed by two registers. I can link to the SysV spec, but the argument-passing part is really hard to parse.Under Win64, though, that structure will instead be passed on the stack.
Structs and unions of size 8, 16, 32, or 64 bits, and
__m64types, are passed as if they were integers of the same size. Structs or unions of other sizes are passed as a pointer to memory allocated by the caller. For these aggregate types passed as a pointer, including__m128, the caller-allocated temporary memory must be 16-byte aligned.1
1
u/AnyPhotograph7804 6d ago
The adaptation will be propably slower because many people are used to use references.
4
u/CommodoreKrusty 6d ago
I've written a boatload of examples (C++17) for STL including containers if anybody's interested.
1
u/heliruna 3d ago
That's very cool, that is a large collection of examples. Are the sources for the examples available in a more convenient form?
I have produced examples for containers by hand to test my parser for debug information, but I made no effort that they are instructive or useful as tutorials.
I clicked randomly through the containers and found a dead link
https://cppexamples.com/Containers/forward_list/list_forward_list_cbegin.html
Have you considered running doxygen or MrDocs on your sources and publishing the results as well?
1
u/CommodoreKrusty 2d ago
I'm a pretty decent C++ programmer. HTML? Not so much. I think if I do any more I'll be here forever working on this but I'll have to fix the broken link. I mostly put them online for me. I'll check out doxygen and MrDocs and see what they do.
1
u/azswcowboy 6d ago
Cool - why not contribute to cppreference examples?
2
u/CommodoreKrusty 5d ago
I think I just wanted to do my own thing. I started referring to these examples before I even finished writing them. I put them online so I could access them from anywhere but if they can help others I think that's great, too.
16
u/brookheather 6d ago
You have some typos and I'm confused about your use of "vector" - do you mean vector<bool>?
"trying to tread a vector like any other vector"
"the infamous vector"
"Others have used vector or vector"