 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Erik Arner Guest
|
Posted: Thu Nov 18, 2004 12:06 am Post subject: Template base class qualifier needed, why?? |
|
|
Ah, the joys of upgrading to a higher gcc version... It *always* exposes
some flaw in my conception of C++. This time it's illustrated by the
example below. I'd be very glad if someone could explain what's going on.
Thanks,
Erik
<CODE>
template<class T>
class A
{
public:
T a;
};
template <class T>
class B : public A<T>
{
public:
T b;
B()
{
b = a;//Compiles with g++ 3.3, not with 3.4. Why??
b = A<T>::a;//Compiles with both
}
};
int main()
{
return 0;
}
</CODE>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Thu Nov 18, 2004 12:19 pm Post subject: Re: Template base class qualifier needed, why?? |
|
|
Erik Arner wrote:
| Quote: | Ah, the joys of upgrading to a higher gcc version... It *always* exposes
some flaw in my conception of C++. This time it's illustrated by the
example below. I'd be very glad if someone could explain what's going on.
template<class T
class A
{
public:
T a;
};
template
class B : public A
{
public:
T b;
B()
{
b = a;//Compiles with g++ 3.3, not with 3.4. Why??
b = A
}
};
|
That's the rule about unqualified names. With the new g++, you must
qualify names that do not resolve in the template's lexical scope.
In your example, the 'a' is one such name, so you must qualify it.
Another 'fix' is to use "this->a".
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Nov 18, 2004 12:22 pm Post subject: Re: Template base class qualifier needed, why?? |
|
|
"Erik Arner" <erik_arner (AT) hotmail (DOT) com> wrote...
| Quote: |
Ah, the joys of upgrading to a higher gcc version... It *always* exposes
some flaw in my conception of C++. This time it's illustrated by the
example below. I'd be very glad if someone could explain what's going on.
Thanks,
Erik
CODE
template<class T
class A
{
public:
T a;
};
template
class B : public A
{
public:
T b;
B()
{
b = a;//Compiles with g++ 3.3, not with 3.4. Why??
b = A
|
'a' is a dependent name because its existence depends on T.
Base classes are not automatically searched to resolve dependent
names. You have to fully qualify the name to let the compiler
know where to get it.
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michiel Salters Guest
|
Posted: Fri Nov 19, 2004 12:54 pm Post subject: Re: Template base class qualifier needed, why?? |
|
|
Erik Arner <erik_arner (AT) hotmail (DOT) com> wrote
| Quote: | template<class T
struct A
{
T a;
};
template
struct B : public A
{
T b;
B() {
b = a;//Compiles with g++ 3.3, not with 3.4. Why??
b = A
}
};
|
The short answer is that the first name lookup of a (unqualified)
happens when B is compiled. Because the base class is not known
at that point (T is still open) the name 'a' cannot be found.
The second name lookup "stalls" because the compiler sees A<T>::
Those name lookups are done when a particular instance of B is
instantiated. A similar effect can be achieved by writing this->a
The relevant part of the standard is chapter 14 on "dependent names"
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 |
|
 |
Maxim Yegorushkin Guest
|
Posted: Fri Nov 19, 2004 12:58 pm Post subject: Re: Template base class qualifier needed, why?? |
|
|
On 17 Nov 2004 19:06:40 -0500, Erik Arner <erik_arner (AT) hotmail (DOT) com> wrote:
| Quote: |
Ah, the joys of upgrading to a higher gcc version... It *always* exposes
some flaw in my conception of C++. This time it's illustrated by the
example below. I'd be very glad if someone could explain what's going on.
Thanks,
Erik
CODE
template<class T
class A
{
public:
T a;
};
template
class B : public A
{
public:
T b;
B()
{
b = a;//Compiles with g++ 3.3, not with 3.4. Why??
b = A
}
};
int main()
{
return 0;
}
/CODE
|
That is because tho-phase name look up has been introduced in gcc 3.4. In
conforming code one can't access members of a template dependent base
class without fully qualifying that member name.
Googling groups for "two phase lookup" yields a wealth of information.
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler (ne Spange Guest
|
Posted: Fri Nov 19, 2004 1:00 pm Post subject: Re: Template base class qualifier needed, why?? |
|
|
Hello Erik Arner,
Erik Arner schrieb:
| Quote: | CODE
template<class T
class A
{
public:
T a;
};
template
class B : public A
{
public:
T b;
B()
{
b = a;//Compiles with g++ 3.3, not with 3.4. Why??
b = A
}
};
int main()
{
return 0;
}
/CODE
|
g++ 3.4 is correct to reject the code under the assumption that your
program somehow
instantiates a B<> type (which it currently does). To conformingly
**provoke** the compiler error
your example should have been modified to something like
| Quote: | CODE
template<class T
class A
{
public:
T a;
};
template
class B : public A
{
public:
T b;
B()
{
b = a;//Compiles with g++ 3.3, not with 3.4. Why??
b = A
}
};
int main()
{
B<int> item; // Try to instantiate an B<int |
The reason for the error is easy to understand if you consider the
possibility of template
specializations. Consider the following further modified example code:
| Quote: | CODE
template
class A
{
public:
T a;
};
template
class B : public A
{
public:
T b;
B()
{
b = a;//Compiles with g++ 3.3, not with 3.4. Why??
b = A
// a which allows the wanted assignment |
class A<int> {};
| Quote: | int main()
{
B<int> item; // Does not compile successfully any more, because |
A<int> does not have
// any accessible member a
So we have the situation here, that the specialization of class template
A with type int
does not have any member a at all! (Or we could even define such a
member which would
not allow the assignment we want to do).
HTH,
Daniel Krügler
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
|
| Back to top |
|
 |
Joshua Lehrer Guest
|
Posted: Fri Nov 19, 2004 4:03 pm Post subject: Re: Template base class qualifier needed, why?? |
|
|
Erik Arner <erik_arner (AT) hotmail (DOT) com> wrote
| Quote: | template<class T
class A
{
public:
T a;
};
template
class B : public A
{
public:
T b;
B()
{
b = a;//Compiles with g++ 3.3, not with 3.4. Why??
b = A
}
};
|
Because the compiler doesn't know that "a" is a member of the base
class. At the instantiation time, A<U> may have been specialized and
the data member "a" removed:
template <> class A<int> { };
B<int> b; //error
You need to explicitly state/enforce that "a" be a member of the base
class, either via "A<T>::a" or "this->a".
joshua lehrer
factset research systems
NYSE:FDS
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Balog Pal Guest
|
Posted: Fri Nov 19, 2004 4:59 pm Post subject: Re: Template base class qualifier needed, why?? |
|
|
"Erik Arner" <erik_arner (AT) hotmail (DOT) com> wrote
| Quote: | b = a;//Compiles with g++ 3.3, not with 3.4. Why??
b = A<T>::a;//Compiles with both
|
Welcome to 2phase lookup.
For the future remember to access base class members using this-> instead of
just writing them in a template.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| 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
|
|