Derived template classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Torsten Wiebesiek

    #1

    Derived template classes

    Hi folks,

    I have have upgraded my compiler (gcc 3.3 -> 3.4) and the new version
    performs two-stage (or dependent) name lookup:



    Given a templated base class:

    template <class T> struct Base
    {
    int f();
    int i;
    };

    Following derived class doesn't compile anymore:

    template <class T> struct Derived : public Base<T>
    {
    int g() { return i; }
    int h() { return f();}
    };


    The correct implementation is:

    template <class T> struct Derived : public Base<T>
    {
    int g() { return Base<T>::i; }
    int h() { return Base<T>::f();}
    };

    This looks quite confusing, especially when you combine more members of the
    base class in an expression.

    The gcc docs suggest: "Alternativ ely, Base<T>::i might be brought into scope
    by a using-declaration."

    And here is my problem: what is the correct syntax of this using
    declaration?

    "using Base<T>::i;" doesn't work?

    Regards,
    Torsten
  • Victor Bazarov

    #2
    Re: Derived template classes

    Torsten Wiebesiek wrote:[color=blue]
    > I have have upgraded my compiler (gcc 3.3 -> 3.4) and the new version
    > performs two-stage (or dependent) name lookup:
    >
    > http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html
    >
    > Given a templated base class:
    >
    > template <class T> struct Base
    > {
    > int f();
    > int i;
    > };
    >
    > Following derived class doesn't compile anymore:
    >
    > template <class T> struct Derived : public Base<T>
    > {
    > int g() { return i; }
    > int h() { return f();}
    > };
    >
    >
    > The correct implementation is:
    >
    > template <class T> struct Derived : public Base<T>
    > {
    > int g() { return Base<T>::i; }
    > int h() { return Base<T>::f();}[/color]

    Or

    int g() { return this->i; }
    int h() { return this->f(); }
    [color=blue]
    > };
    >
    > This looks quite confusing, especially when you combine more members of the
    > base class in an expression.[/color]

    Yes, it can look confusing.
    [color=blue]
    > The gcc docs suggest: "Alternativ ely, Base<T>::i might be brought into scope
    > by a using-declaration."
    >
    > And here is my problem: what is the correct syntax of this using
    > declaration?
    >
    > "using Base<T>::i;" doesn't work?[/color]

    Is that a question? I mean, doesn't it? Please post the code in
    its non-working shape. I got this, and it should be working fine:

    template <class T> struct Base
    {
    int f();
    int i;
    };

    template <class T> struct Derived : public Base<T>
    {
    using Base<T>::i;
    using Base<T>::f;
    int g() { return i; }
    int h() { return f();}
    };

    int main()
    {
    Derived<int> di;
    di.g();
    di.h();
    }

    V

    Comment

    • mlimber

      #3
      Re: Derived template classes


      Torsten Wiebesiek wrote:[color=blue]
      > Hi folks,
      >
      > I have have upgraded my compiler (gcc 3.3 -> 3.4) and the new version
      > performs two-stage (or dependent) name lookup:
      >
      > http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html
      >
      > Given a templated base class:
      >
      > template <class T> struct Base
      > {
      > int f();
      > int i;
      > };
      >
      > Following derived class doesn't compile anymore:
      >
      > template <class T> struct Derived : public Base<T>
      > {
      > int g() { return i; }
      > int h() { return f();}
      > };
      >
      >
      > The correct implementation is:
      >
      > template <class T> struct Derived : public Base<T>
      > {
      > int g() { return Base<T>::i; }
      > int h() { return Base<T>::f();}
      > };
      >
      > This looks quite confusing, especially when you combine more members of the
      > base class in an expression.
      >
      > The gcc docs suggest: "Alternativ ely, Base<T>::i might be brought into scope
      > by a using-declaration."
      >
      > And here is my problem: what is the correct syntax of this using
      > declaration?
      >
      > "using Base<T>::i;" doesn't work?[/color]

      That should work at function or file scope, not class scope. Are you
      sure it's in the right place? You could also do:

      int g() { return this->i; }

      Cheers! --M

      Comment

      • mlimber

        #4
        Re: Derived template classes

        mlimber wrote:[color=blue]
        > Torsten Wiebesiek wrote:[color=green]
        > > Hi folks,
        > >
        > > I have have upgraded my compiler (gcc 3.3 -> 3.4) and the new version
        > > performs two-stage (or dependent) name lookup:
        > >
        > > http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html
        > >
        > > Given a templated base class:
        > >
        > > template <class T> struct Base
        > > {
        > > int f();
        > > int i;
        > > };
        > >
        > > Following derived class doesn't compile anymore:
        > >
        > > template <class T> struct Derived : public Base<T>
        > > {
        > > int g() { return i; }
        > > int h() { return f();}
        > > };
        > >
        > >
        > > The correct implementation is:
        > >
        > > template <class T> struct Derived : public Base<T>
        > > {
        > > int g() { return Base<T>::i; }
        > > int h() { return Base<T>::f();}
        > > };
        > >
        > > This looks quite confusing, especially when you combine more members of the
        > > base class in an expression.
        > >
        > > The gcc docs suggest: "Alternativ ely, Base<T>::i might be brought into scope
        > > by a using-declaration."
        > >
        > > And here is my problem: what is the correct syntax of this using
        > > declaration?
        > >
        > > "using Base<T>::i;" doesn't work?[/color]
        >
        > That should work at function or file scope, not class scope. Are you
        > sure it's in the right place? You could also do:
        >
        > int g() { return this->i; }
        >
        > Cheers! --M[/color]

        Retraction: Victor is right. It should work at class scope. Post the
        code.

        Cheers! --M

        Comment

        • Torsten Wiebesiek

          #5
          Re: Derived template classes

          Victor Bazarov wrote:[color=blue][color=green]
          >> And here is my problem: what is the correct syntax of this using
          >> declaration?
          >>
          >> "using Base<T>::i;" doesn't work?[/color]
          >
          > Is that a question? I mean, doesn't it? Please post the code in
          > its non-working shape.[/color]

          Sorry, you're right. The wrong code is in the function definition:

          template <class T>
          int Derived<T>::g()
          {
          using Base<T>::i;
          return i;
          }

          template <class T>
          int Derived<T>::h()
          {
          using Base<T>::f;
          return f();
          }
          [color=blue]
          > [snip]
          >
          > template <class T> struct Derived : public Base<T>
          > {
          > using Base<T>::i;
          > using Base<T>::f;
          > int g() { return i; }
          > int h() { return f();}
          > };
          >
          > [snip][/color]

          I thought, the using declaration might work at function scope, but somehow
          it isn't. At class scope it works fine. :-)

          Thanks for your help, Victor.

          Torsten

          Comment

          Working...