------- Additional Comments From michel at colorado dot edu 2005-09-16 03:49 ------- Subject: Re: Template subclasses do not find variables in their parent classes
I read this. What I am questioning is the logic behind this. Isn't the logic of template that if you were to replace the typenames with the actual types thing should work as if you had typed it with the actual type? This thing in the standard makes no sense to me. I do not see any logic for it and as the comment says, many code will need to be modified and thousands of lines revised and some may totally escape the process as the example shows with the variable n and the function g. pinskia at gcc dot gnu dot org wrote: > ------- Additional Comments From pinskia at gcc dot gnu dot org 2005-09-16 > 03:36 ------- > (In reply to comment #2) > >> No idea why that is. What's the logic behind this? Make my code twice as >> long? >> > > The standard says this, did you read the page I gave? If not then I copied > and pasted what is relevant to > your issue: > > 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 (); > } > }; > > -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23908