From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 285863858031; Fri, 30 Jul 2021 19:11:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 285863858031 From: "ammiera at hotmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/101698] New: Template type conversion operator from base class preferred over matching overload Date: Fri, 30 Jul 2021 19:11:04 +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: 11.1.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: ammiera at hotmail 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: Fri, 30 Jul 2021 19:11:05 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D101698 Bug ID: 101698 Summary: Template type conversion operator from base class preferred over matching overload Product: gcc Version: 11.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: ammiera at hotmail dot com Target Milestone: --- There are two conversion operators, template and non-templated. Their return type might match: virtual operator const std::string&() const; template operator const T&() const; If so, when trying to call it, the template is always preferred over non-template and there's no way to SFINAE out of it: #include #include #include class Base { public: template operator const T&() const { std::cout << "use template method" << std::endl; static T tmp{}; return tmp; } virtual operator const std::string&() const { std::cout << "use overload method" << std::endl; const static std::string tmp; return tmp; } }; template class Derive : public Base { public: operator const T&() const override { using Y =3D std::string; static_assert(std::is_same::value, ""); std::string static res; res =3D Base::operator const Y&(); res =3D Base::operator const T&(); return res; } }; int main() { Derive a; const std::string& b =3D a; (void)b; } Reproduced locally on: gcc (SUSE Linux) 11.1.1 20210721 [revision 076930b9690ac3564638636f6b13bbb6bc608aea] Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Did a quick bisection via godbolt.org, the last version it's didn't occur = is 7.5. https://gcc.godbolt.org/z/KcbK9vv5z=