Qt and Trivial Relocation (Part 6)
AI summary AI-generated, may contain mistakes
This article, part of a series on Qt and trivial relocation, describes an experiment using C++26 reflection to make Qt's trivial relocation support easier and safer. The author shows that reflection can automatically detect trivially relocatable types (like simple Rule-of-Zero classes), eliminating the need for the error-prone Q_DECLARE_TYPEINFO macro, and can validate manually marked types—finding real mistakes in Qt. However, the approach struggles with third-party types that can't be introspected, and automatic detection risks binary compatibility problems when different compilation modes disagree about a type's relocatability.
Key takeaways
- C++26 will ship without trivial relocation, as P2786 was merged and then removed from the draft.
- C++26 reflection can automatically detect trivially relocatable types, such as simple Rule-of-Zero classes like Person, without needing Qt's macro.
- Reflection-based checks can validate hand-marked types and already found a mistaken marking plus several missing markings in Qt's codebase.
- Types with user-provided special member functions still need manual marking, and third-party types like std::string, std::unique_ptr, and containers remain difficult to handle.
- Automatic detection introduces binary compatibility risks, since libraries compiled in different modes may disagree on whether a type is trivially relocatable.
Qt and Trivial Relocation (Part 6)
An experiment with C++26 reflection
It's been a while since the last instalment of this series! Back then, I wrote that having trivial relocation in C++26 was "doable". Well... it didn't quite go that way: P2786 was merged into the C++26 draft in February 2025, and then removedin November of the same year. C++26 is going to ship without trivial relocation.
Out of respect for the author only the opening is shown here. Read the rest on the original site, or in HorizonReader with highlights, notes and AI help.