public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/99192] New: A wrong Aggregate initialization for a union with a variant member of non-aggregate class type
@ 2021-02-22  9:25 xmh970252187 at gmail dot com
  2021-03-03 20:46 ` [Bug c++/99192] " mpolacek at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: xmh970252187 at gmail dot com @ 2021-02-22  9:25 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 99192
           Summary: A wrong Aggregate initialization for a union with a
                    variant member of non-aggregate class type
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xmh970252187 at gmail dot com
  Target Milestone: ---

struct X { const int a; int b; X(int):a(0){}};
union Y {  X x;int k; };
int main(){
    Y y{ };
}

Such an example is accepted by GCC(https://godbolt.org/z/hTn8zc) but rejected
by Clang. GCC has a wrong interpretation for this example.  

Union `Y` is an aggregate class type as per [dcl.init.aggr#1], So aggregate
initialization is applied to this declaration `Y y{ };`. According to 
> If the aggregate is a union and the initializer list is empty, then
>> if any variant member has a default member initializer, that member is initialized from its default member initializer;
>> otherwise, the first member of the union (if any) is copy-initialized from an empty initializer list.

Since there's no default-initializer for a member of Y, hence the second bullet
applies here. That means, the first member will be copy-initialized from an
empty initializer list. However, the class `X` is not an aggregate class type
and it has a user-defined constructor `X(int)`, Hence the following rule will
be applied to initialize `x`, that is:  
>Otherwise, if T is a class type, constructors are considered. The applicable constructors are enumerated and the best one is chosen through overload resolution ([over.match], [over.match.list]). If a narrowing conversion (see below) is required to convert any of the arguments, the program is ill-formed.

The only candidate function here is `X(int)` and the corresponding argument
list is empty. So, there's no viable function that exists, Hence the invocation
should be ill-formed here as per:  
> If a best viable function exists and is unique, overload resolution succeeds and produces it as the result. Otherwise overload resolution fails and the invocation is ill-formed.

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

* [Bug c++/99192] A wrong Aggregate initialization for a union with a variant member of non-aggregate class type
  2021-02-22  9:25 [Bug c++/99192] New: A wrong Aggregate initialization for a union with a variant member of non-aggregate class type xmh970252187 at gmail dot com
@ 2021-03-03 20:46 ` mpolacek at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-03-03 20:46 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-03-03
                 CC|                            |mpolacek at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Confirmed.  I think this might be too risky to change in GCC 11.  Not a
regression, either.

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

end of thread, other threads:[~2021-03-03 20:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-22  9:25 [Bug c++/99192] New: A wrong Aggregate initialization for a union with a variant member of non-aggregate class type xmh970252187 at gmail dot com
2021-03-03 20:46 ` [Bug c++/99192] " mpolacek 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).