From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27464 invoked by alias); 17 Feb 2011 12:23:02 -0000 Received: (qmail 27452 invoked by uid 22791); 17 Feb 2011 12:23:00 -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 X-Spam-Check-By: sourceware.org Received: from mail-iw0-f175.google.com (HELO mail-iw0-f175.google.com) (209.85.214.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 17 Feb 2011 12:22:55 +0000 Received: by iwn8 with SMTP id 8so2442010iwn.20 for ; Thu, 17 Feb 2011 04:22:53 -0800 (PST) MIME-Version: 1.0 Received: by 10.42.228.199 with SMTP id jf7mr2597152icb.459.1297945373406; Thu, 17 Feb 2011 04:22:53 -0800 (PST) Received: by 10.42.230.68 with HTTP; Thu, 17 Feb 2011 04:22:53 -0800 (PST) In-Reply-To: <201102171203.53710.pascal@francq.info> References: <201102171203.53710.pascal@francq.info> Date: Thu, 17 Feb 2011 13:09:00 -0000 Message-ID: Subject: Re: Strange behavior with templates and G++ From: Jonathan Wakely To: Pascal Francq Cc: gcc-help 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/msg00257.txt.bz2 [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 =A0= void Super::Test(C*) [with C =3D A1] > But here the call refers clearly to the second method. The error still ap= pears > if A1 and A2 do not inherit from a same root class. If I replace the code= 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: void Test(A1*) {} }; class A2 { public: void Test(A2*) {} }; class Super2 : public A1, public A2 { }; void Test(void) { Super2 T; A1* ptr; T.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. That makes the lookup ill-formed, before overload resolution is attempted.