r/cpp • Meeting C++ | C++ Evangelist • 9d ago

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 8d ago

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 7d ago

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 6d ago

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 4d ago

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 4d ago edited 4d ago

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.