Type names defined in base template class not visible in derived class in gcc3.2?

Discussion in 'C++' started by James Ying, Oct 28, 2003.

  1. James Ying

    James Ying Guest

    Following is the complete code demonstrating the issue:
    =====================
    > cat tt.cc

    #include <iostream>

    template <class T>
    class Base {
    public:
    typedef T difference_type;
    };

    template <class T>
    class Derived : public Base<T> {
    public:
    void test() {
    difference_type t = 10;
    std::cout << "t is " << t << std::endl;
    }
    };

    int main()
    {

    Derived<int> d;
    d.test();

    }
    ========== end of code ======================

    This code compiles without warning in g++2.96, MIPSpro C++ Compilers:
    Version 7.3.1.1m, Sun WorkShop 6 update 2 C++ 5.3 Patch 111685-10.
    However g++3.2 gives warning: implicit typename is deprecated, please
    see the documentation for details.

    My question: is the warning necessary according to the C++ standard?
    While I haven't read the standard carefully enough to answer the
    question definitely, the C++PL book gives a similar code in the class
    of Checked Iterator (pp. 562-563, 3rd editon):

    template<class Cont, class Iter = typename Cont::iterator>
    class Check_iter : public iterator_traits<Iter> {
    // ....
    reference_type operator*() {
    // ...

    Note the reference_type (rather, reference) is type-defined in the
    base template iterator_traits<Iter>, and used directly in the derived
    class without prefixing "typename".

    One obvious approach to get rid of the g++ warning is to add the
    following code in the derived class:

    typedef typename Base<T>::difference_type difference_type;
     
    James Ying, Oct 28, 2003
    #1
    1. Advertising

  2. Re: Type names defined in base template class not visible in derivedclass in gcc3.2?

    James Ying wrote:

    > Following is the complete code demonstrating the issue:
    > =====================
    >> cat tt.cc

    > #include <iostream>
    >
    > template <class T>
    > class Base {
    > public:
    > typedef T difference_type;
    > };
    >
    > template <class T>
    > class Derived : public Base<T> {
    > public:
    > void test() {
    > difference_type t = 10;
    > std::cout << "t is " << t << std::endl;
    > }
    > };
    >
    > int main()
    > {
    >
    > Derived<int> d;
    > d.test();
    >
    > }
    > ========== end of code ======================
    >
    > This code compiles without warning in g++2.96, MIPSpro C++ Compilers:
    > Version 7.3.1.1m, Sun WorkShop 6 update 2 C++ 5.3 Patch 111685-10.
    > However g++3.2 gives warning: implicit typename is deprecated, please
    > see the documentation for details.
    >
    > My question: is the warning necessary according to the C++ standard?
    > While I haven't read the standard carefully enough to answer the
    > question definitely, the C++PL book gives a similar code in the class
    > of Checked Iterator (pp. 562-563, 3rd editon):


    I'd say that it should be an error, not just a warning. According to the
    definition given in 14.6.2.1/1, name 'difference_type' used in the
    definition of method 'test()' is not a dependent name. For this reason,
    as 14.6/8 says, this name should be looked up is accordance with usual
    name lookup rules at the point of template definition. Since class
    template B<T> does not participate in this name lookup process (see
    14.6.2/3), the name 'difference_type' will not be found and the code is
    ill-formed. That's actually how Comeau's compiler treats this code - it
    issues an error

    "ComeauTest.c", line 13: error: identifier "difference_type" is undefined
    difference_type t = 10;
    ^

    > template<class Cont, class Iter = typename Cont::iterator>
    > class Check_iter : public iterator_traits<Iter> {
    > // ....
    > reference_type operator*() {
    > // ...


    > Note the reference_type (rather, reference) is type-defined in the
    > base template iterator_traits<Iter>, and used directly in the derived
    > class without prefixing "typename".


    Well, the code in the book can be incomplete or outdated.

    > One obvious approach to get rid of the g++ warning is to add the
    > following code in the derived class:
    >
    > typedef typename Base<T>::difference_type difference_type;


    That would be the right thing to do.

    --
    Best regards,
    Andrey Tarasevich
    Brainbench C and C++ Programming MVP
     
    Andrey Tarasevich, Oct 28, 2003
    #2
    1. Advertising

  3. James Ying

    James Ying Guest

    Thank you for pointing to the right sections in the standard, and I
    agree with you. The standard is pretty clean on this, although it
    makes life a bit mroe difficult -- the supposed trick presented in
    TC+PL about checked iterator will not do the trick to save a few
    typings.


    Andrey Tarasevich <> wrote in message news:<>...
    > James Ying wrote:
    >
    > > Following is the complete code demonstrating the issue:
    > > =====================
    > >> cat tt.cc

    > > #include <iostream>
    > >
    > > template <class T>
    > > class Base {
    > > public:
    > > typedef T difference_type;
    > > };
    > >
    > > template <class T>
    > > class Derived : public Base<T> {
    > > public:
    > > void test() {
    > > difference_type t = 10;
    > > std::cout << "t is " << t << std::endl;
    > > }
    > > };
    > >
    > > int main()
    > > {
    > >
    > > Derived<int> d;
    > > d.test();
    > >
    > > }
    > > ========== end of code ======================
    > >
    > > This code compiles without warning in g++2.96, MIPSpro C++ Compilers:
    > > Version 7.3.1.1m, Sun WorkShop 6 update 2 C++ 5.3 Patch 111685-10.
    > > However g++3.2 gives warning: implicit typename is deprecated, please
    > > see the documentation for details.
    > >
    > > My question: is the warning necessary according to the C++ standard?
    > > While I haven't read the standard carefully enough to answer the
    > > question definitely, the C++PL book gives a similar code in the class
    > > of Checked Iterator (pp. 562-563, 3rd editon):

    >
    > I'd say that it should be an error, not just a warning. According to the
    > definition given in 14.6.2.1/1, name 'difference_type' used in the
    > definition of method 'test()' is not a dependent name. For this reason,
    > as 14.6/8 says, this name should be looked up is accordance with usual
    > name lookup rules at the point of template definition. Since class
    > template B<T> does not participate in this name lookup process (see
    > 14.6.2/3), the name 'difference_type' will not be found and the code is
    > ill-formed. That's actually how Comeau's compiler treats this code - it
    > issues an error
    >
    > "ComeauTest.c", line 13: error: identifier "difference_type" is undefined
    > difference_type t = 10;
    > ^
    >
    > > template<class Cont, class Iter = typename Cont::iterator>
    > > class Check_iter : public iterator_traits<Iter> {
    > > // ....
    > > reference_type operator*() {
    > > // ...

    >
    > > Note the reference_type (rather, reference) is type-defined in the
    > > base template iterator_traits<Iter>, and used directly in the derived
    > > class without prefixing "typename".

    >
    > Well, the code in the book can be incomplete or outdated.
    >
    > > One obvious approach to get rid of the g++ warning is to add the
    > > following code in the derived class:
    > >
    > > typedef typename Base<T>::difference_type difference_type;

    >
    > That would be the right thing to do.
     
    James Ying, Oct 28, 2003
    #3
    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. Matt Graham
    Replies:
    0
    Views:
    568
    Matt Graham
    Jul 21, 2003
  2. Andy Lomax
    Replies:
    5
    Views:
    487
    John Carson
    Jun 30, 2005
  3. Replies:
    1
    Views:
    407
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    398
    Victor Bazarov
    May 23, 2007
  5. Hicham Mouline
    Replies:
    1
    Views:
    604
    Victor Bazarov
    Apr 20, 2009
Loading...

Share This Page