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

2

u/LB-- Professional+Hobbyist Sep 25 '26

I'm still waiting for a way to disconnect memory layout from construct/destruct order without bypassing normal language features like constructors and destructors. It's really frustrating to have to have padding just to have the correct construct/destruct order.

1

u/Able_Development2478 Sep 26 '26

Declaration order in C++ strictly dictates the memory layout and the initialisation order and lot of code depends on it so I doubt there will be a change which seperates memory layout and initialisation order. There will be a lot of pushback I think. Unless I am misunderstanding something :D

1

u/LB-- Professional+Hobbyist Sep 27 '26

I mean adding a way to opt-into separating them. All existing code would be unaffected. Not sure how that could warrant pushback.

1

u/Able_Development2478 Sep 29 '26

That'll change the ABI, won't it? We can keep the syntax the same but I think declaring something would not mean the same. I can't picture it tbh

1

u/LB-- Professional+Hobbyist Sep 29 '26 edited Sep 29 '26

The ABI is only affected by the memory layout. The construct/destruct order only needs to be known by the constructors and destructors, which don't have to be defined inline. I can imagine a new type of declaration/definition combo, allowing you to either define the order inline in the class body, or to declare that it will be defined out-of-line, and must be defined before constructors/destructors need it. So for example you could add the forward declaration of custom order to the header, then define the full order in the source file above the constructors and destructors. No ABI breakage in that case. Alternatively, each constructor and destructor could declare its own preferred order, but that might be tricky for reasons beyond my consideration.

1

u/yuri-kilochek Sep 27 '26

You can wrap each member in an anonymous union to suppress automatic construction/destruction and construct/destruct manually in any order.

1

u/LB-- Professional+Hobbyist Sep 27 '26

That's the "bypassing normal language features like constructors and destructors" part of my comment. It works, but the ergonomics are awful, and you have to be especially careful about exceptions.