templates and inheritance

Andrew Pinski pinskia@physics.uc.edu
Tue May 16 04:22:00 GMT 2006


On May 15, 2006, at 9:15 PM, Bar-Tov Aharon-R53349 wrote:

> hello,
> something that seems like a problem (havn't found something in the  
> bug base).

>
> template <class T>
> class t_base
> {
> public:
>   T *ptr;
> };
>
> template <class T>
> class t_derived : public t_base<T>
> {
> public:
>   t_derived() { ptr = NULL; }
> };

This is described on the changes page for 3.4:
http://gcc.gnu.org/gcc-3.4/changes.html:

In a template definition, unqualified names will no longer find  
members of a dependent base (as specified by [temp.dep]/3 in the C++  
standard). For example,
	template <typename T> struct B {
	  int m;
	  int n;
	  int f ();
	  int g ();
	};
	int n;
	int g ();
	template <typename T> struct C : B<T> {
	  void h ()
	  {
	    m = 0; // error
	    f ();  // error
	    n = 0; // ::n is modified
	    g ();  // ::g is called
	  }
	};
You must make the names dependent, e.g. by prefixing them with this- 
 >. Here is the corrected definition of C<T>::h,

	template <typename T> void C<T>::h ()
	{
	  this->m = 0;
	  this->f ();
	  this->n = 0
	  this->g ();
	}
As an alternative solution (unfortunately not backwards compatible  
with GCC 3.3), you may use using declarations instead of this->:

	template <typename T> struct C : B<T> {
	  using B<T>::m;
	  using B<T>::f;
	  using B<T>::n;
	  using B<T>::g;
	  void h ()
	  {
	    m = 0;
	    f ();
	    n = 0;
	    g ();
	  }
	};

-- Pinski



More information about the Gcc-bugs mailing list