https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110416

            Bug ID: 110416
           Summary: Error initializing of const union variable with a
                    mutable field
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fchelnokov at gmail dot com
  Target Milestone: ---

This program

union U {
    mutable int x;
    float y = 2;
};

int main() {
    const U u;
    u.x = 1;
}

is accepted in Clang and MSVC, but GCC complains:

<source>: In function 'int main()':
<source>:7:13: error: uninitialized 'const u' [-fpermissive]
    7 |     const U u;
      |             ^
<source>:1:7: note: 'const union U' has no user-provided default constructor
    1 | union U {
      |       ^
<source>:2:17: note: and the implicitly-defined constructor does not initialize
'int U::x'
    2 |     mutable int x;

Online demo: https://gcc.godbolt.org/z/T8bqYTG5f

Reply via email to