 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
tthunder@gmx.de Guest
|
Posted: Tue Nov 29, 2005 10:19 am Post subject: Must use "this->" with templated code (g++)??? |
|
|
Hi there,
I am developing some kind of template classes for different
compilers... now I have got a problem compiling with g++ (or: Dev-C++
4.9.9.2)
Think about this code:
------------------
class Base
{
public:
int h;
};
template<typename T>
class myClass : public T
{
public:
myClass() {h = 6;} // <---- MOST important thing
};
int main(int argc, char *argv[])
{
myClass
}
------------------
It compiles well on MSVC .NET or Borland C++ Builder 6.0
But Dev-C++ reports this error:
'h' undeclared (first use this function)
So I have included "this->":
------------------
class Base
{
public:
int h;
};
template<typename T>
class myClass : public T
{
public:
myClass() {this->h = 6;} // <---- MOST important thing
};
int main(int argc, char *argv[])
{
myClass
}
------------------
Well, why? I don't want to go through my code and add "this->"
everywhere. There must be a good reason for that and I want to know, of
course. Any solutions?
Thanks for help and advice,
Kirsten
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Anthony Williams Guest
|
Posted: Tue Nov 29, 2005 2:00 pm Post subject: Re: Must use "this->" with templated code (g++)??? |
|
|
[email]tthunder (AT) gmx (DOT) de[/email] writes:
| Quote: | class Base
{
public:
int h;
};
template<typename T
class myClass : public T
{
public:
myClass() {h = 6;} // <---- MOST important thing
};
int main(int argc, char *argv[])
{
myClass
}
------------------
It compiles well on MSVC .NET or Borland C++ Builder 6.0
But Dev-C++ reports this error:
'h' undeclared (first use this function)
So I have included "this->":
Well, why? I don't want to go through my code and add "this->"
everywhere. There must be a good reason for that and I want to know, of
course. Any solutions?
|
This is caused by two-phase lookup of names in templates, and the fact that
the variable h is declared in the base class, which is dependent on the
template parameter.
Unqualified names such as "h" are looked up in the first phase, in which names
in dependent bases are ignored, as this is done at the point of declaration of
the template, and the type of T is not known.
Qualified names, such as "T::h", or names referenced through a dependent
expression, such as "this->h" are looked up in phase 2, when the template is
instantiated, and then the actual type of T is known, so members of dependent
bases are also searched.
Older compilers often look up all names at the point of instantiation, so your
original code compiles, but this is not the standard-specified behaviour.
Anthony
--
Anthony Williams
Software Developer
Just Software Solutions Ltd
http://www.justsoftwaresolutions.co.uk
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Richter Guest
|
Posted: Tue Nov 29, 2005 2:05 pm Post subject: Re: Must use "this->" with templated code (g++)??? |
|
|
Hi,
| Quote: | I am developing some kind of template classes for different
compilers... now I have got a problem compiling with g++ (or: Dev-C++
4.9.9.2)
Think about this code:
------------------
class Base
{
public:
int h;
};
template<typename T
class myClass : public T
{
public:
myClass() {h = 6;} // <---- MOST important thing
|
This *should* not compile.
| Quote: | };
int main(int argc, char *argv[])
{
myClass
}
------------------
It compiles well on MSVC .NET or Borland C++ Builder 6.0
But Dev-C++ reports this error:
'h' undeclared (first use this function)
So I have included "this->":
|
Exactly, you have to. This is the "two-phase lookup" for templates. The
compiler *must not* resolve h from the type T here.
| Quote: | Well, why? I don't want to go through my code and add "this->"
everywhere. There must be a good reason for that and I want to know, of
course. Any solutions?
|
Yes, go thru your code and add "this->". (-; Sorry, but it's what
the standard says here.
Alternatives: Make "h" dependent on the type "T", i.e. use
T::h = 6;
or, third alternative (though not backwards compatible to pre-3.4.x gcc's)
use "using":
template<typename T>
class myClass : public T
{
using T::h;
public:
....
}
Other than that, you would have to check your compiler for
non-standard extensions. g++ has a "permissive" flag to do that.
So long,
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Peter Kümmel Guest
|
|
| Back to top |
|
 |
Arne Schmitz Guest
|
|
| Back to top |
|
 |
Michiel.Salters@tomtom.co Guest
|
Posted: Tue Nov 29, 2005 3:56 pm Post subject: Re: Must use "this->" with templated code (g++)??? |
|
|
[email]tthunder (AT) gmx (DOT) de[/email] wrote:
| Quote: | class Base
{
public:
int h;
};
template<typename T
class myClass : public T
{
public:
myClass() {h = 6;} // <---- MOST important thing
};
|
| Quote: | Dev-C++ reports this error:
'h' undeclared (first use this function)
So I have included "this->":
|
| Quote: | template<typename T
class myClass : public T
{
public:
myClass() {this->h = 6;} // <---- MOST important thing
};
|
| Quote: |
Well, why? I don't want to go through my code and add "this->"
everywhere. There must be a good reason for that and I want to know, of
course. Any solutions?
|
"Two-phase name lookup" Templates are compiled when they're first seen.
Every name that does not depend on the template parameter is looked up
in that phase. 'h' doesn't depend, so it's looked up, but not found.
In your second example, 'this' does depend on T, because it has type
myClass<T>* . h also depends on T because it occurs on the right hand
side of a -> and the left side already depends on T.
The basic rule here is to prefix this-> whenever you use a member of a
base class and the base class type depends on your template parameter.
(e.g. both myClass : public T and myClass : public Base<T> )
In this case, you can also write T::h instead of h. That means the same
as this->h, and the compiler agains notes the dependency on T.
HTH,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Steven E. Harris Guest
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|