https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123246
Bug ID: 123246
Summary: New expression ignores the explicit specifier for
list-initialization
Product: gcc
Version: 15.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: nedimsehic50 at gmail dot com
Target Milestone: ---
struct S1 {
explicit S1() {}
};
struct S2 {
S1 s1;
};
int main() {
// S2 s{}; // Gives error as expected.
new S2{}; // Expected an error, but passes.
}
Godbolt: https://godbolt.org/z/PxafGs118
[expr.new/24.2] (https://eel.is/c++draft/expr.new#24.2) says that we need to
follow the initialization rules from [dcl.init] for direct-initialization.
Following those rules, we get to list-initialization, then
aggregate-initialization. We deduce that there are no explicitly initialized
elements, so we end up at [dcl.init.aggr/5.2]
(https://eel.is/c++draft/dcl.init.aggr#5.2):
> Otherwise, if the element is not a reference, the element is copy-initialized
> from an empty initializer list ([dcl.init.list]).
We have 1 element (s1), and are copy-initializing it from an empty initializer
list. That is ill-formed because an explicit constructor is chosen. However,
that doesn't happen and the code gets accepted.