public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/102247] New: Overload resolution with brace-init is ambiguous when it shouldn't be
@ 2021-09-08 22:49 ldionne.2 at gmail dot com
  2021-09-08 22:55 ` [Bug c++/102247] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: ldionne.2 at gmail dot com @ 2021-09-08 22:49 UTC (permalink / raw)
  To: gcc-bugs

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++.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/102247] Overload resolution with brace-init is ambiguous when it shouldn't be
  2021-09-08 22:49 [Bug c++/102247] New: Overload resolution with brace-init is ambiguous when it shouldn't be ldionne.2 at gmail dot com
@ 2021-09-08 22:55 ` pinskia at gcc dot gnu.org
  2021-09-09 12:29 ` rs2740 at gmail dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-08 22:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102247

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I think this the same case as in
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84849#c5 .

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/102247] Overload resolution with brace-init is ambiguous when it shouldn't be
  2021-09-08 22:49 [Bug c++/102247] New: Overload resolution with brace-init is ambiguous when it shouldn't be ldionne.2 at gmail dot com
  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
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: rs2740 at gmail dot com @ 2021-09-09 12:29 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102247

TC <rs2740 at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rs2740 at gmail dot com

--- Comment #2 from TC <rs2740 at gmail dot com> ---
struct X { explicit X() {} };
struct Y { Y() {} };

void f(X);
void f(Y);

void g() {
    f({}); // GCC rejects, clang accepts
}

GCC is correct. For copy-list-initialization, explicitness of constructor is
not considered in forming implicit conversion sequences; rather, if a explicit
constructor is chosen, the program is ill-formed. See core issue 1228 and 
[over.match.list].

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/102247] Overload resolution with brace-init is ambiguous when it shouldn't be
  2021-09-08 22:49 [Bug c++/102247] New: Overload resolution with brace-init is ambiguous when it shouldn't be ldionne.2 at gmail dot com
  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
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-09 20:23 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102247

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=70637

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This looks almost exactly the same as PR 70637.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/102247] Overload resolution with brace-init is ambiguous when it shouldn't be
  2021-09-08 22:49 [Bug c++/102247] New: Overload resolution with brace-init is ambiguous when it shouldn't be ldionne.2 at gmail dot com
                   ` (2 preceding siblings ...)
  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
  5 siblings, 0 replies; 7+ messages in thread
From: rs2740 at gmail dot com @ 2021-09-09 20:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102247

--- Comment #4 from TC <rs2740 at gmail dot com> ---
See also PR 60027 and its duplicates.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/102247] Overload resolution with brace-init is ambiguous when it shouldn't be
  2021-09-08 22:49 [Bug c++/102247] New: Overload resolution with brace-init is ambiguous when it shouldn't be ldionne.2 at gmail dot com
                   ` (3 preceding siblings ...)
  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
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-14  5:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102247

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 97220 at least.

*** This bug has been marked as a duplicate of bug 97220 ***

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/102247] Overload resolution with brace-init is ambiguous when it shouldn't be
  2021-09-08 22:49 [Bug c++/102247] New: Overload resolution with brace-init is ambiguous when it shouldn't be ldionne.2 at gmail dot com
                   ` (4 preceding siblings ...)
  2021-12-14  5:57 ` pinskia at gcc dot gnu.org
@ 2022-12-03 18:52 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-03 18:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102247

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---


*** This bug has been marked as a duplicate of bug 60027 ***

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-12-03 18:52 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-08 22:49 [Bug c++/102247] New: Overload resolution with brace-init is ambiguous when it shouldn't be ldionne.2 at gmail dot com
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

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).