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 class with template baseclass with member template

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





PostPosted: Thu Sep 25, 2003 8:53 pm    Post subject: Template class with template baseclass with member template Reply with quote



Hi,

I was wondering if any Gurus out there can tell me why this doesnt work,
and perhaps a way to make it work:

template<typename T>
class B
{
public:
template<int I>
void doStuff(){
}
};

template<typename T>
class C : public B<T>
{
public:
void doOtherStuff(){
doStuff<2>(); //Mysterious parse error from g++ 3.2
}
};

//end code
It works when either C or B or doStuff() isnt paramaterized, but not when
all three are. Sounds like a mystery is afoot.

Long Time Reader First Time Poster (LTRFTP)
Thanks!

Mike Lin

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





PostPosted: Fri Sep 26, 2003 10:35 am    Post subject: Re: Template class with template baseclass with member templ Reply with quote



This works with gcc 2.95.2.

7 using namespace std;
8
9 template <class T>
10 class B {
11 public:
12 template <int I>
13 void doStuff() {
14 cout << "B 15 }
16
17 };
18
19 template 20 class C : public B<T> {
21 public:
22 void doOtherStuff() {
23 this->template doStuff<2>();
24 }
25
26 };
27
28 class T {
29 };
30
31 int main()
32 {
33 C<T> c;
34 c.doOtherStuff();
35 return 0;
36 }


"miclin" <miclin (AT) interchange (DOT) ubc.ca> wrote

Quote:
Hi,

I was wondering if any Gurus out there can tell me why this doesnt work,
and perhaps a way to make it work:

template<typename T
class B
{
public:
template void doStuff(){
}
};

template class C : public B {
public:
void doOtherStuff(){
doStuff<2>(); //Mysterious parse error from g++ 3.2
}
};

//end code
It works when either C or B or doStuff() isnt paramaterized, but not when
all three are. Sounds like a mystery is afoot.




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

Back to top
johnchx
Guest





PostPosted: Sat Sep 27, 2003 12:33 am    Post subject: Re: Template class with template baseclass with member templ Reply with quote



miclin <miclin (AT) interchange (DOT) ubc.ca> wrote

Quote:
template<typename T
class B
{
public:
template void doStuff(){
}
};

template class C : public B {
public:
void doOtherStuff(){
doStuff<2>(); //Mysterious parse error from g++ 3.2
}
};

YATPLUP! ("Yet Another Two-Phase Look-Up Problem") :-)

The compiler can't figure out what to do about the name doStuff<2> for
two reasons:

First, doStuff is a non-dependent name, so the compiler has to look it
up at the point of the template's declaration. Non-dependent names
are *not* looked up in the scope of dependent base classes (like B<T>,
which depends on the template parameter T). So the first thing we
need to do is make doStuff a dependent name, by prefixing either
this-> or the name of the base class to search (B<T>:Smile, like this:

this->doStuff<2>(); // almost fixed

or

B<T>::doStuff<2>(); // almost fixed

Next, now that we've made doStuff a dependent name, the compiler can't
look it up when parsing the template declaration, so it has no way of
determining whether it names a template (i.e. whether doStuff<2> is a
template-id or a typo). So we have to add the template keyword as a
hint:

this->template doStuff<2>(); // works
B<T>::template doStuff<2>(); // works

I prefer the form that explicitly names the base class to be searched,
simply because I think it expresses the intent more clearly (and makes
the compiler enforce that intent). Others seem to prefer the this->
form. They both work fine in this case.

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

Back to top
Hyman Rosen
Guest





PostPosted: Sat Sep 27, 2003 12:40 am    Post subject: Re: Template class with template baseclass with member templ Reply with quote

miclin wrote:
Quote:
It works when either C or B or doStuff() isnt paramaterized, but not when
all three are. Sounds like a mystery is afoot.

Two-phase name lookup. Dependent base classes aren't searched for
non-dependent names in the template definition, so the doStuff in
doOtherStuff is undefined. Say B<T>::template doStuff<2>(); instead.


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

Back to top
Siemel Naran
Guest





PostPosted: Sat Sep 27, 2003 12:45 am    Post subject: Re: Template class with template baseclass with member templ Reply with quote

"miclin" <miclin (AT) interchange (DOT) ubc.ca> wrote in message

Quote:
template<typename T
class B
{
public:
template void doStuff(){
}
};

template class C : public B {
public:
void doOtherStuff(){
doStuff<2>(); //Mysterious parse error from g++ 3.2
}
};

In a template class, when calling a function from base class and the base
class is a template, you have to qualify the base class name. Let us know
if the following works,

void doOtherStuff(){
B<T>::template doStuff<2>();
}

--
+++++++++++
Siemel Naran


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

Back to top
Thomas Mang
Guest





PostPosted: Sat Sep 27, 2003 1:29 am    Post subject: Re: Template class with template baseclass with member templ Reply with quote



miclin schrieb:

Quote:
Hi,

I was wondering if any Gurus out there can tell me why this doesnt work,
and perhaps a way to make it work:

template<typename T
class B
{
public:
template void doStuff(){
}
};

template class C : public B {
public:
void doOtherStuff(){
doStuff<2>(); //Mysterious parse error from g++ 3.2
}
};

Change the offending line to

this->template doStuff<2>();


and it works fine.



regards,

Thomas


[ 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.