Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Pete
Guest
 
Posts: n/a
#1: Jul 22 '05
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();
}


John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

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


Rob Williscroft
Guest
 
Posts: n/a
#3: Jul 22 '05

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/
Sharad Kala
Guest
 
Posts: n/a
#4: Jul 22 '05

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






Pete
Guest
 
Posts: n/a
#5: Jul 22 '05

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.
Closed Thread


Similar C / C++ bytes