[clang] [clang] Allow class with anonymous union member to be const-default-constructible even if a union member has a default member initializer (#95854) (PR #96301)

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 25 12:56:34 PDT 2024


================
@@ -1392,7 +1392,8 @@ class CXXRecordDecl : public RecordDecl {
   bool allowConstDefaultInit() const {
     return !data().HasUninitializedFields ||
            !(data().HasDefaultedDefaultConstructor ||
-             needsImplicitDefaultConstructor());
+             needsImplicitDefaultConstructor()) ||
+           hasInClassInitializer();
----------------
zygoloid wrote:

I don't think so. Maybe we can change the behavior of `HasUninitializedFields` -- it only exists to support this check, so we can change it to do whatever this check needs. What I'm thinking is:

* If the class is not a union, then proceed as we currently do: start it off as `false` and set it to `true` if we find an uninitialized field.
* If the class is a union, then do the opposite: start it off as `true` and set it to `false` if we find an initialized field.

(So for a non-union, the field is true if all fields are const default initializable, and for a union, the field is true if *any* field is const default initializable.)

Then I think the only change we'd need here would be to also treat empty unions as const default initializable.

https://github.com/llvm/llvm-project/pull/96301


More information about the cfe-commits mailing list