From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 917643857817; Wed, 8 Sep 2021 22:49:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 917643857817 From: "ldionne.2 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/102247] New: Overload resolution with brace-init is ambiguous when it shouldn't be Date: Wed, 08 Sep 2021 22:49:47 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: ldionne.2 at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Sep 2021 22:49:47 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D102247 Bug ID: 102247 Summary: Overload resolution with brace-init is ambiguous when it shouldn't be Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: ldionne.2 at gmail dot com Target Milestone: --- The following code fails to compile when I believe it should succeed (https://godbolt.org/z/z9aved8Wb): #include template struct pair { template constexpr pair(U1&&, U2&&) { } }; struct BraceInit { BraceInit() =3D default; }; struct ExplicitBraceInit { explicit ExplicitBraceInit() =3D default; }; constexpr int f(pair) { return 1;= } constexpr int f(pair) { return 2; } static_assert(f({{}, {}}) =3D=3D 2, ""); Indeed, the error is :15:16: error: call of overloaded 'f()' is ambiguous 15 | static_assert(f({{}, {}}) =3D=3D 2, ""); | ~^~~~~~~~~~ :12:15: note: candidate: 'constexpr int f(pair)' 12 | constexpr int f(pair) { retu= rn 1; } | ^ :13:15: note: candidate: 'constexpr int f(pair)' 13 | constexpr int f(pair) { return 2; } | ^ Compiler returned: 1 I think it should succeed because `f(pair)` can never be selected, since selecting it would require using the explicit constructor of `ExplicitBraceInit`. And indeed, if we tr= y to call `f(pair)` alone, we get (https://godbolt.org/z/W33Pvnnoe): :15:16: error: converting to 'ExplicitBraceInit' from initializ= er list would use explicit constructor 'constexpr ExplicitBraceInit::ExplicitBraceInit()' 15 | static_assert(f({{}, {}}) =3D=3D 2, ""); | ~^~~~~~~~~~ Compiler returned: 1 So I'm not sure why that overload is considered valid for overload resoluti= on (leading to the ambiguity), but it seems like GCC itself doesn't think it's valid when there are no other candidates. Also note that Clang compiles this code without issue, calling the non-explicit BraceInit version as expected. This issue was uncovered while implementing http://wg21.link/P1951 in libc+= +.=