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

Discussion in 'C++' started by Pete, Jul 22, 2004.

  1. Pete

    Pete Guest

    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();
    }
     
    Pete, Jul 22, 2004
    #1
    1. Advertising

  2. "Pete" <> wrote in message
    news:AsPLc.48097$...
    > 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
    > }
    >


    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
     
    John Harrison, Jul 22, 2004
    #2
    1. Advertising

  3. Pete wrote in news:AsPLc.48097$ in
    comp.lang.c++:

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


    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.

    >
    > template<typename T>
    > struct base
    > {
    > T m_x;
    > };
    >
    > template<typename T>
    > struct derived : public base<T>
    > {


    Fix 1:

    using base< T >::m_x;

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


    Fix 2:

    > T b = base<T>::m_x; // OK
    > T a = m_x; // error: `m_x' undeclared


    Fix 3:

    T c = this->m_x;

    > }
    >


    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/
     
    Rob Williscroft, Jul 22, 2004
    #3
  4. Pete

    Sharad Kala Guest

    "Pete" <> wrote in message
    news:AsPLc.48097$...
    > 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();
    > }


    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-lite/containers-and-templates.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
     
    Sharad Kala, Jul 22, 2004
    #4
  5. Pete

    Pete Guest

    Sharad Kala wrote:

    > 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-lite/containers-and-templates.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.
    >


    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.
     
    Pete, Jul 22, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandre Tolmos
    Replies:
    1
    Views:
    387
    Victor Bazarov
    Aug 8, 2003
  2. John Harrison
    Replies:
    18
    Views:
    567
    Pete Becker
    Aug 11, 2003
  3. flopbucket
    Replies:
    2
    Views:
    443
    Andrey Tarasevich
    Jun 23, 2006
  4. chhenning
    Replies:
    5
    Views:
    387
    chhenning
    Feb 13, 2008
  5. Bhawna
    Replies:
    7
    Views:
    494
    Bhawna
    Aug 26, 2008
Loading...

Share This Page