From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 823F53858D20; Sat, 3 Dec 2022 16:48:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 823F53858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670086102; bh=NgQ2XMDa4tBN+Sk6KXozV7x9CTQPxtkDFGdQuEtvaU4=; h=From:To:Subject:Date:From; b=wqc9fJpp0LtzO2X+3V9yle/na7085Cbisydwg318zCEkXIlZtTFKEQlSQ8MQ38GxT czOWt+eH+92zRcDB4EWRQpOOE5R2Pai560dimQ8xhFc4y0u95iXe8ay9YzpnXdB7Qu koFPcFJkgnI0QA7gyYrRIyWejRASNlXoiIuayq3E= From: "nruslan_devel at yahoo dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/107958] New: Ambiguity with uniform initialization in overloaded operator and explicit constructor Date: Sat, 03 Dec 2022 16:48:22 +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: nruslan_devel at yahoo 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107958 Bug ID: 107958 Summary: Ambiguity with uniform initialization in overloaded operator and explicit constructor Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: nruslan_devel at yahoo dot com Target Milestone: --- Suppose we have the following example with a class that has to keep two pointers. (I actually encountered this error with a more complex example, b= ut this one is just for illustration.) The problem arises when I attempt to use the assignment operator and curly braces. If I understand correctly, two possibilities exist when passing curly brace= s: 1. Use the overloaded operator=3D (implicitly convert curly braces to a pai= r). In this particular example, we could have probably used make_pair but I deliberately put curly braces to show how this error is triggered. 2. Use the constructor to create a new PairPtr instance and then copy it to= the old object through operator=3D Both clang and gcc complain unless I mark the corresponding constructor as 'explicit'. To avoid the ambiguity with the second case, I mark the corresponding constructor as 'explicit' and expect that the overloaded operator=3D to be used. That seems to work with clang/llvm but not with gcc= (see the error below). #include #include struct PairPtr { PairPtr() {} PairPtr(const PairPtr &s) { a =3D s.a; b =3D s.b; } explicit PairPtr(int *_a, int *_b) { a =3D _a; b =3D _b; } PairPtr& operator=3D(const PairPtr &s) { a =3D s.a; b =3D s.b; return *this; } PairPtr& operator=3D(const std::pair& pair) { a =3D pair.first; b =3D pair.second; return *this; } int *a; int *b; }; void func(int *a, int *b) { PairPtr p; p =3D { a, b }; } Error (note that clang/llvm can compile the above code successfully!): Note that 'explicit' for the constructor fixes the problem for clang/llvm b= ut does not fix the problem for gcc. 2.cpp: In function =E2=80=98void func(int*, int*)=E2=80=99: 2.cpp:38:20: error: ambiguous overload for =E2=80=98operator=3D=E2=80=99 (o= perand types are =E2=80=98PairPtr=E2=80=99 and =E2=80=98=E2= =80=99) 38 | p =3D { a, b }; | ^ 2.cpp:18:18: note: candidate: =E2=80=98PairPtr& PairPtr::operator=3D(const = PairPtr&)=E2=80=99 18 | PairPtr& operator=3D(const PairPtr &s) { | ^~~~~~~~ 2.cpp:24:18: note: candidate: =E2=80=98PairPtr& PairPtr::operator=3D(const std::pair&)=E2=80=99 24 | PairPtr& operator=3D(const std::pair& pair) { |=