C++ Logo

std-discussion

Advanced search

Re: Value initialization of class with list initialization, explicit ctors

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Fri, 09 Feb 2024 13:40:58 +0000
On Fri, 2024-02-09 at 14:01 +0100, Johannes Schaub via Std-Discussion
wrote:
> According to what I read on the Eelis draft, the following should
> resolve to the nonexplicit ctor (consistency gone, since it is
> different for non default ctors... what is the reason for this
> inconsistency?).
>
> But GCC and Clang raise an ambiguity. Why do they? This seems like a
> simple example.
>
> struct A { explicit A() { } A(int = 0) { } }; A a = {};

Overload resolution for copy-list-initialization is intentionally
different from overload resolution for copy-initialization.

https://eel.is/c++draft/dcl.init.list#3.5 applies. Value initialization
resolves to direct initialization from `()`, which is ambiguous.

This is mostly consistent with
https://eel.is/c++draft/dcl.init.list#3.7
and https://eel.is/c++draft/over.match.list where all constructors are
considered (explicit and non-explicit) and is ill-formed in case of
ambiguity or if the most viable candidate is explicit in case of copy-
initialization.

However I don't immediately see what would prevent the explicit default
constructor to be selected if it was the only viable one. Maybe
[dcl.init.list]/3.5 should also refer to [over.match.list] explicitly.

Cheers,
Lénárd

Received on 2024-02-09 13:41:04