public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/110416] New: Error initializing of const union variable with a mutable field
@ 2023-06-26 17:14 fchelnokov at gmail dot com
  2023-06-26 20:19 ` [Bug c++/110416] Error initializing of const aggregate " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: fchelnokov at gmail dot com @ 2023-06-26 17:14 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/110416] Error initializing of const aggregate variable with a mutable field
  2023-06-26 17:14 [Bug c++/110416] New: Error initializing of const union variable with a mutable field fchelnokov at gmail dot com
@ 2023-06-26 20:19 ` pinskia at gcc dot gnu.org
  2023-06-26 20:23 ` [Bug c++/110416] Error initializing of const union " pinskia at gcc dot gnu.org
  2023-06-26 20:31 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-26 20:19 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Error initializing of const |Error initializing of const
                   |union variable with a       |aggregate variable with a
                   |mutable field               |mutable field

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
struct also fails ...

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/110416] Error initializing of const union variable with a mutable field
  2023-06-26 17:14 [Bug c++/110416] New: Error initializing of const union variable with a mutable field fchelnokov at gmail dot com
  2023-06-26 20:19 ` [Bug c++/110416] Error initializing of const aggregate " pinskia at gcc dot gnu.org
@ 2023-06-26 20:23 ` pinskia at gcc dot gnu.org
  2023-06-26 20:31 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-26 20:23 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
            Summary|Error initializing of const |Error initializing of const
                   |aggregate variable with a   |union variable with a
                   |mutable field               |mutable field
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2023-06-26

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #1)
> struct also fails ...

Well clang does not reject that case but MSVC does.
So I don't think the struct case is a GCC bug; just clang does not have a
check.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/110416] Error initializing of const union variable with a mutable field
  2023-06-26 17:14 [Bug c++/110416] New: Error initializing of const union variable with a mutable field fchelnokov at gmail dot com
  2023-06-26 20:19 ` [Bug c++/110416] Error initializing of const aggregate " pinskia at gcc dot gnu.org
  2023-06-26 20:23 ` [Bug c++/110416] Error initializing of const union " pinskia at gcc dot gnu.org
@ 2023-06-26 20:31 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-26 20:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
[dcl.init.general]/8 (specifically 8.2) is what makes the union case valid.

Though I wonder what mutable field means in an union if not the whole union is
not mutable ...

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-06-26 20:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-26 17:14 [Bug c++/110416] New: Error initializing of const union variable with a mutable field fchelnokov at gmail dot com
2023-06-26 20:19 ` [Bug c++/110416] Error initializing of const aggregate " pinskia at gcc dot gnu.org
2023-06-26 20:23 ` [Bug c++/110416] Error initializing of const union " pinskia at gcc dot gnu.org
2023-06-26 20:31 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).