C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template base class qualifier needed, why??

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Erik Arner
Guest





PostPosted: Thu Nov 18, 2004 12:06 am    Post subject: Template base class qualifier needed, why?? Reply with 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 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





PostPosted: Thu Nov 18, 2004 12:19 pm    Post subject: Re: Template base class qualifier needed, why?? Reply with quote



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





PostPosted: Thu Nov 18, 2004 12:22 pm    Post subject: Re: Template base class qualifier needed, why?? Reply with quote



"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





PostPosted: Fri Nov 19, 2004 12:54 pm    Post subject: Re: Template base class qualifier needed, why?? Reply with quote

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





PostPosted: Fri Nov 19, 2004 12:58 pm    Post subject: Re: Template base class qualifier needed, why?? Reply with quote

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





PostPosted: Fri Nov 19, 2004 1:00 pm    Post subject: Re: Template base class qualifier needed, why?? Reply with quote

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


Quote:
return 0;
}

/CODE


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


Quote:
}
};


template

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

Quote:
return 0;
}

/CODE


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





PostPosted: Fri Nov 19, 2004 1:01 pm    Post subject: Re: Template base class qualifier needed, why?? Reply with quote

Erik Arner 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 }
};

Just read the changelog Wink
http://gcc.gnu.org/gcc-3.4/changes.html

Uli

--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Joshua Lehrer
Guest





PostPosted: Fri Nov 19, 2004 4:03 pm    Post subject: Re: Template base class qualifier needed, why?? Reply with quote

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





PostPosted: Fri Nov 19, 2004 4:59 pm    Post subject: Re: Template base class qualifier needed, why?? Reply with quote

"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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.