GCC now supports C++26 trivial unions per P3074R7 and CWG3189.
GCC now implements the C++26 trivial unions feature, allowing unions with simpler initialization and destruction semantics.
GCC now supports C++26’s trivial unions as defined by P3074R7 and CWG3189. This impacts how the compiler handles unions, particularly regarding default constructors and destructors. The patch also fixes a pre-existing issue in handling const-qualified variant members in older C++ standards, where the compiler incorrectly diagnosed their usage.
In Details
This commit implements P3074R7 (trivial unions) and CWG3189 (implicitly deleted destructors for union-like classes) in GCC. The primary changes are in cp/method.cc, affecting how the compiler handles default initialization and destruction of unions, including variant members. The change to walk_field_subobs relaxes checks for variant members, addressing inconsistencies in how const members were treated before C++26.
For Context
In C++, a union is a special kind of class that can hold different data types in the same memory location. Trivial unions in C++26 simplify the rules around how these unions are initialized and destroyed, making them easier to use in certain contexts. This commit makes GCC compliant with the new C++26 standard regarding unions, improving code compatibility and potentially enabling new optimization opportunities.