From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23827 invoked by alias); 17 Feb 2011 14:08:34 -0000 Received: (qmail 23816 invoked by uid 22791); 17 Feb 2011 14:08:33 -0000 X-SWARE-Spam-Status: No, hits=-2.3 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: sourceware.org Received: from mail-iy0-f175.google.com (HELO mail-iy0-f175.google.com) (209.85.210.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 17 Feb 2011 14:08:28 +0000 Received: by iyj8 with SMTP id 8so2469976iyj.20 for ; Thu, 17 Feb 2011 06:08:27 -0800 (PST) MIME-Version: 1.0 Received: by 10.42.241.70 with SMTP id ld6mr2860309icb.124.1297951706961; Thu, 17 Feb 2011 06:08:26 -0800 (PST) Received: by 10.42.230.68 with HTTP; Thu, 17 Feb 2011 06:08:26 -0800 (PST) In-Reply-To: <20110217131616.GU5274@axel> References: <201102171203.53710.pascal@francq.info> <20110217131616.GU5274@axel> Date: Thu, 17 Feb 2011 16:30:00 -0000 Message-ID: Subject: Re: Strange behavior with templates and G++ From: Jonathan Wakely To: gcc-help@gcc.gnu.org Cc: Axel Freyn Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2011-02/txt/msg00261.txt.bz2 On 17 February 2011 13:16, Axel Freyn wrote: > On Thu, Feb 17, 2011 at 12:22:53PM +0000, Jonathan Wakely wrote: >> [Moved from the gcc list] >> >> On 17 February 2011 11:03, Pascal Francq wrote: >> > Hi, >> > While compiling the following code, I got an error : >> > >> > >> > template class Super >> > { >> > public: >> > =A0 =A0 =A0 =A0Super(void) {} >> > =A0 =A0 =A0 =A0void Test(C*) {} >> > }; >> > >> > class A >> > { >> > public: >> > =A0 =A0 =A0 =A0A(void) {} >> > }; >> > >> > class A1 : public A >> > { >> > public: >> > =A0 =A0 =A0 =A0A1(void) : A() {} >> > }; >> > >> > class A2 : public A >> > { >> > public: >> > =A0 =A0 =A0 =A0A2(void) : A() {} >> > }; >> > >> > class Super2 : public Super, public Super >> > { >> > public: >> > =A0 =A0 =A0 =A0Super2(void) {} >> > }; >> > >> > void Test(void) >> > { >> > =A0 =A0 =A0 =A0Super2 T; >> > =A0 =A0 =A0 =A0A1* ptr; >> > =A0 =A0 =A0 =A0T.Test(ptr); >> > } >> > >> > >> > The compiler gives me the following error for the Test() function: >> > =A0 =A0 =A0 =A0error: request for member =91Test=92 is ambiguous >> > =A0 =A0 =A0 =A0error: candidates are: =A0void Super::Test(C*) [with= C =3D A2] >> > =A0 =A0 =A0 =A0error: =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0void Super::Test(C*) [with C =3D A1] >> > But here the call refers clearly to the second method. The error still= appears >> > if A1 and A2 do not inherit from a same root class. If I replace the c= ode with >> > an explicit call it works: >> > =A0 =A0 =A0 =A0T.Super::Test(ptr) >> > But this make the code less cleaner. >> > >> > Is this a problem related to a misunderstood concept from me, a wrong >> > implementation or a technical problem of g++ ? >> >> >> The ambiguity has nothing to do with templates, the code can be reduced = to: >> >> class A1 >> { >> public: >> =A0 =A0 =A0 =A0void Test(A1*) {} >> }; >> >> class A2 >> { >> public: >> =A0 =A0 =A0 =A0void Test(A2*) {} >> }; >> >> class Super2 : public A1, public A2 >> { >> }; >> >> void Test(void) >> { >> =A0 =A0 =A0 =A0Super2 T; >> =A0 =A0 =A0 =A0A1* ptr; >> =A0 =A0 =A0 =A0T.Test(ptr); >> } >> >> This is covered by 10.2 [class.member.lookup] in the C++ standard, >> which says that a name is ambiguous if found in more than one base >> class. =A0That makes the lookup ill-formed, before overload resolution >> is attempted. > > In addition: it is not necessary to use everywhere an explicit call. > You can import the function by adding "using"-definitions. With: > class Super2 : public A1, public A2 > { > =A0public: > =A0 =A0using A1::Test; > =A0 =A0using A2::Test; > }; > you can call T.Test(ptr) directly, and C++ will do correct overload > resolution -- both with and without templates Right, in that case name lookup finds two overloads of "Test" without looking in any base classes, then overload resolution selects the right one.