r/cpp • Meeting C++ | C++ Evangelist • Sep 24 '26

About alignment, struct layout and the cache...

https://meetingcpp.com/blog/items/About-alignment--struct-layout-and-the-cache---.html
36 Upvotes

27 comments sorted by

View all comments

Show parent comment

3

u/SirClueless Sep 25 '26

By the way, I'm looking forward very much to having std::indirect in the language, because it makes the design of the container described in this article pretty much free: The "dynamic array of pointers" container described here is exactly std::vector<std::indirect<T>>.

When you want pointer-stability and a small object to iterate over, this type of design is already frequently recommended. For example, Abseil recommends using std::unique_ptr<T> when you want pointer stability of values: https://abseil.io/docs/cpp/guides/container. But using std::unique_ptr comes with all kinds of compromises (especially, no more copy constructor, but also doesn't hash or compare like a value type any more which is also a killer that rules it out for the keys of maps and sets). Now with std::indirect choosing whether to put the value inline in the container is nearly a free choice.

2

u/azswcowboy Sep 25 '26

Great points about indirect. There’s a couple implementations of indirect with the one in Beman project working with c++17 since indirect doesn’t really need c++2x features.