[Insight-developers] FW: [Vxl-maintainers] Compile errors with gcc 3.4.1

Miller, James V (Research) millerjv at crd.ge.com
Fri Sep 10 13:51:47 EDT 2004


Here is some insight that Brad provided to the vxl crew.

Jim

-----Original Message-----
From: Brad King [mailto:brad.king at kitware.com]
Sent: Tuesday, August 03, 2004 8:14 AM
To: Peter.Vanroose at esat.kuleuven.ac.be
Cc: vxl-maintainers at lists.sourceforge.net
Subject: Re: [Vxl-maintainers] Compile errors with gcc 3.4.1


Peter Vanroose wrote:
> Does anybody of you have an idea (1) why the following would not be 
> standard C++, and (2) if so, how to elegantly solve this problem, since 
> there are 200 places in vxl where gcc 3.4.1 gives this compile error.
> 
> --    Peter.
> 
> -----------------
> template <class T> class A { public: void foo() {} };
> template <class T> class B : public A<T> { void bar() { foo(); } };

I know I've seen this before with the very strict Comeau compiler, and 
in fact the compiler is correct to give this error.  A quick search of 
the C++98 Standard found these:

14.6.2/3
In the definition of a class template or in the definition of a member
of such a template that appears outside of the template definition, if
a base class of this template depends on a templateparameter, the base
class scope is not examined during name lookup until the class
template is instantiated.

14.6.2/4
If a base class is a dependent type, a member of that class cannot
hide a name declared within a template, or a name from the template's
enclosing scopes.

I'm not sure why the compiler is trying to lookup the name foo before B 
is instantiated, but basically the problem is that foo is a member of a 
base class that is dependent on a template parameter, so it is not 
considered to be in-scope within the definition of bar.  We can add a 
using declaration to make it visible:

template <class T> class A { public: void foo() {} };
template <class T> class B : public A<T>
{
   using A<T>::foo;
   public: void bar() { foo(); }
};

Alternatively the A<T> qualifier can be added to the call:

template <class T> class A { public: void foo() {} };
template <class T> class B : public A<T>
{
public: void bar() { A<T>::foo(); }
};

However, I think the best choice is to add "this->":

template <class T> class A { public: void foo() {} };
template <class T> class B : public A<T>
{
public: void bar() { this->foo(); }
};

Since B is a class template, the name "this" is dependent on the 
template argument T, so the lookup of the name "foo" is delayed until 
the point of instantiation, at which point the base class names become 
available.  Since adding "this->" is valid for non-templates too, we 
could make its use a standard convention in vxl and the problem will go 
away.

-Brad



-------------------------------------------------------
This SF.Net email is sponsored by OSTG. Have you noticed the changes on
Linux.com, ITManagersJournal and NewsForge in the past few weeks? Now,
one more big change to announce. We are now OSTG- Open Source Technology
Group. Come see the changes on the new OSTG site. www.ostg.com
_______________________________________________
Vxl-maintainers mailing list
Vxl-maintainers at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vxl-maintainers


More information about the Insight-developers mailing list