Connecting Tech Pros Worldwide Help | Site Map

What's wrong here? Accessing members of a templated base class

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 04:45 PM
Pete
Guest
 
Posts: n/a
Default What's wrong here? Accessing members of a templated base class

If anyone here can tell me what's going wrong in this snippet, I'd be much
obliged. It works under g++ 3.3 but not the (fussier) g++ 3.4. Thanks...



template<typename T>
struct base
{
T m_x;
};

template<typename T>
struct derived : public base<T>
{
void bleh();
};

template<typename T>
void derived<T>::bleh()
{
T b = base<T>::m_x; // OK
T a = m_x; // error: `m_x' undeclared
}

int main()
{
derived<int> c;
c.bleh();
}


  #2  
Old July 22nd, 2005, 04:45 PM
John Harrison
Guest
 
Posts: n/a
Default Re: What's wrong here? Accessing members of a templated base class


"Pete" <youmustbekidding@spamsrus.co.uk> wrote in message
news:AsPLc.48097$y04.21385@fe2.news.blueyonder.co. uk...[color=blue]
> If anyone here can tell me what's going wrong in this snippet, I'd be much
> obliged. It works under g++ 3.3 but not the (fussier) g++ 3.4. Thanks...
>
>
>
> template<typename T>
> struct base
> {
> T m_x;
> };
>
> template<typename T>
> struct derived : public base<T>
> {
> void bleh();
> };
>
> template<typename T>
> void derived<T>::bleh()
> {
> T b = base<T>::m_x; // OK
> T a = m_x; // error: `m_x' undeclared
> }
>[/color]

It's because 3.4 implements two phase look up correctly and therefore does
not examine the base class when looking up a unqualified dependent name.

Either base<T>::m_x or this->m_x will fix the problem.

John


  #3  
Old July 22nd, 2005, 04:45 PM
Rob Williscroft
Guest
 
Posts: n/a
Default Re: What's wrong here? Accessing members of a templated base class

Pete wrote in news:AsPLc.48097$y04.21385@fe2.news.blueyonder.co. uk in
comp.lang.c++:
[color=blue]
> If anyone here can tell me what's going wrong in this snippet, I'd be
> much obliged. It works under g++ 3.3 but not the (fussier) g++ 3.4.
> Thanks...
>
>[/color]

The name m_x from the base 'base< T >' is dependant on the template
argument 'T', you need to tell a conforming compiler (g++ 3.4 in this
case) that 'm_x' is in 'base< T >' otherwise it will assume its a global.
[color=blue]
>
> template<typename T>
> struct base
> {
> T m_x;
> };
>
> template<typename T>
> struct derived : public base<T>
> {[/color]

Fix 1:

using base< T >::m_x;
[color=blue]
> void bleh();
> };
>
> template<typename T>
> void derived<T>::bleh()
> {[/color]

Fix 2:
[color=blue]
> T b = base<T>::m_x; // OK
> T a = m_x; // error: `m_x' undeclared[/color]

Fix 3:

T c = this->m_x;
[color=blue]
> }
>[/color]

Note that Fix 1 puts a requirement on derived< T > that the
instansiated specialization for 'base< T >' contains a non-type
member m_x, Fix 2 and 3 only make this requirement if
derived< T >::bleh() is instantiated.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
  #4  
Old July 22nd, 2005, 04:45 PM
Sharad Kala
Guest
 
Posts: n/a
Default Re: What's wrong here? Accessing members of a templated base class


"Pete" <youmustbekidding@spamsrus.co.uk> wrote in message
news:AsPLc.48097$y04.21385@fe2.news.blueyonder.co. uk...[color=blue]
> If anyone here can tell me what's going wrong in this snippet, I'd be much
> obliged. It works under g++ 3.3 but not the (fussier) g++ 3.4. Thanks...
>
>
>
> template<typename T>
> struct base
> {
> T m_x;
> };
>
> template<typename T>
> struct derived : public base<T>
> {
> void bleh();
> };
>
> template<typename T>
> void derived<T>::bleh()
> {
> T b = base<T>::m_x; // OK
> T a = m_x; // error: `m_x' undeclared
> }
>
> int main()
> {
> derived<int> c;
> c.bleh();
> }[/color]

That's two phase name lookup. I just checked our FAQ and saw that it is
actually mentioned there.
http://www.parashift.com/c++-faq-lit....html#faq-34.1
7
If you want to know even better then read Part II (Chapters 9 and 10) of C++
Templates (Josuttis/Vandevoorde). They explain it quite well in their book.

-Sharad






  #5  
Old July 22nd, 2005, 04:45 PM
Pete
Guest
 
Posts: n/a
Default Re: What's wrong here? Accessing members of a templated base class

Sharad Kala wrote:
[color=blue]
> That's two phase name lookup. I just checked our FAQ and saw that it is
> actually mentioned there.
>[/color]
http://www.parashift.com/c++-faq-lit....html#faq-34.1[color=blue]
> 7
> If you want to know even better then read Part II (Chapters 9 and 10) of
> C++ Templates (Josuttis/Vandevoorde). They explain it quite well in their
> book.
>[/color]

Ah, the c++-faq. Nice explanation... quite an eye-opener. I'll have to
change a load of code because of that one. Bah.

Thanks for the help, everyone.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.