public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "ldionne.2 at gmail dot com" <gcc-bugzilla@gcc.gnu.org> 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 [thread overview] Message-ID: <bug-102247-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102247 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 <type_traits> template <class T1, class T2> struct pair { template<class U1 = T1, class U2 = T2> constexpr pair(U1&&, U2&&) { } }; struct BraceInit { BraceInit() = default; }; struct ExplicitBraceInit { explicit ExplicitBraceInit() = default; }; constexpr int f(pair<ExplicitBraceInit, ExplicitBraceInit>) { return 1; } constexpr int f(pair<BraceInit, BraceInit>) { return 2; } static_assert(f({{}, {}}) == 2, ""); Indeed, the error is <source>:15:16: error: call of overloaded 'f(<brace-enclosed initializer list>)' is ambiguous 15 | static_assert(f({{}, {}}) == 2, ""); | ~^~~~~~~~~~ <source>:12:15: note: candidate: 'constexpr int f(pair<ExplicitBraceInit, ExplicitBraceInit>)' 12 | constexpr int f(pair<ExplicitBraceInit, ExplicitBraceInit>) { return 1; } | ^ <source>:13:15: note: candidate: 'constexpr int f(pair<BraceInit, BraceInit>)' 13 | constexpr int f(pair<BraceInit, BraceInit>) { return 2; } | ^ Compiler returned: 1 I think it should succeed because `f(pair<ExplicitBraceInit, ExplicitBraceInit>)` can never be selected, since selecting it would require using the explicit constructor of `ExplicitBraceInit`. And indeed, if we try to call `f(pair<ExplicitBraceInit, ExplicitBraceInit>)` alone, we get (https://godbolt.org/z/W33Pvnnoe): <source>:15:16: error: converting to 'ExplicitBraceInit' from initializer list would use explicit constructor 'constexpr ExplicitBraceInit::ExplicitBraceInit()' 15 | static_assert(f({{}, {}}) == 2, ""); | ~^~~~~~~~~~ Compiler returned: 1 So I'm not sure why that overload is considered valid for overload resolution (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++.
next reply other threads:[~2021-09-08 22:49 UTC|newest] Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-09-08 22:49 ldionne.2 at gmail dot com [this message] 2021-09-08 22:55 ` [Bug c++/102247] " pinskia at gcc dot gnu.org 2021-09-09 12:29 ` rs2740 at gmail dot com 2021-09-09 20:23 ` pinskia at gcc dot gnu.org 2021-09-09 20:32 ` rs2740 at gmail dot com 2021-12-14 5:57 ` pinskia at gcc dot gnu.org 2022-12-03 18:52 ` pinskia at gcc dot gnu.org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-102247-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).