https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118228
Bug ID: 118228
Summary: GCC allows instantiation of union with field class
having private constructor
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: wangbopku15 at gmail dot com
Target Milestone: ---
The following code is now accepted by GCC:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
struct A{
private:
A(){}
};
union C{
A a;
int b = 0;
};
int main(){
C c;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Clang, EDG, and ICC reject it for the field 'C::a' is not constructible:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:11:5: error: call to implicitly-deleted default constructor of 'C'
11 | C c;
| ^
<source>:7:5: note: default constructor of 'C' is implicitly deleted because
field 'a' has an inaccessible default constructor
7 | A a;
| ^
1 error generated.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It looks more like GCC is wrong here.
Please see https://godbolt.org/z/o6bf6Kncv