implicit typename problem

Discussion in 'C++' started by xuatla, Jul 19, 2006.

  1. xuatla

    xuatla Guest

    Hi,

    When I compile the following test code I got a warning about implicit
    typename. This happens in the member functions. Do you know the detail
    reason and solution? Thanks.

    - X

    ----------

    test.cpp:29: warning: `cDerived<T>::ptr_type' is implicitly a typename
    test.cpp:29: warning: implicit typename is deprecated, please see the
    documentation for details
    test.cpp:29: warning: `cDerived<T>::ptr_type' is implicitly a typename
    test.cpp:29: warning: implicit typename is deprecated, please see the
    documentation for details

    -----------
    // -- source code: test.cpp

    #include <iostream>
    using namespace std;

    template <typename T>
    class cBase
    {
    public:
    typedef unsigned int size_type;
    typedef T val_type;
    typedef T* ptr_type;

    public:
    cBase()
    {
    // ...
    } ;
    } ;

    template <class T>
    class cDerived : public cBase<T>
    {
    public:
    cDerived()
    {
    // ...
    } ;
    //line 28 here
    ptr_type getMemory(ptr_type& p) // compiling warning: see above
    {
    return p;
    } ;
    } ;

    int main()
    {
    cDerived<double>::size_type i = 0; // OK
    cDerived<double>::val_type d = 0.1; // OK
    cDerived<double>::ptr_type p = new double[10]; // OK

    return 1;
    }
     
    xuatla, Jul 19, 2006
    #1
    1. Advertising

  2. xuatla wrote:
    > When I compile the following test code I got a warning about implicit
    > typename. This happens in the member functions. Do you know the detail
    > reason and solution? Thanks.


    This is covered in the FAQ. Look for "dependent names".

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 19, 2006
    #2
    1. Advertising

  3. xuatla wrote:

    > When I compile the following test code I got a warning about implicit
    > typename. This happens in the member functions. Do you know the detail
    > reason and solution? Thanks.
    >
    > - X
    >
    > ----------
    >
    > test.cpp:29: warning: `cDerived<T>::ptr_type' is implicitly a typename
    > test.cpp:29: warning: implicit typename is deprecated, please see the
    > documentation for details
    > test.cpp:29: warning: `cDerived<T>::ptr_type' is implicitly a typename
    > test.cpp:29: warning: implicit typename is deprecated, please see the
    > documentation for details
    >
    > -----------
    > // -- source code: test.cpp
    >
    > #include <iostream>
    > using namespace std;
    >
    > template <typename T>
    > class cBase
    > {
    > public:
    > typedef unsigned int size_type;
    > typedef T val_type;
    > typedef T* ptr_type;
    >
    > public:
    > cBase()
    > {
    > // ...
    > } ;
    > } ;
    >
    > template <class T>
    > class cDerived : public cBase<T>
    > {
    > public:
    > cDerived()
    > {
    > // ...
    > } ;
    > //line 28 here
    > ptr_type getMemory(ptr_type& p) // compiling warning: see above
    > {
    > return p;
    > } ;
    > } ;
    >
    > int main()
    > {
    > cDerived<double>::size_type i = 0; // OK
    > cDerived<double>::val_type d = 0.1; // OK
    > cDerived<double>::ptr_type p = new double[10]; // OK
    >
    > return 1;
    > }


    The FAQ answers this:

    http://www.parashift.com/c -faq-lite/templates.html#faq-35.18

    I expect you'll find the rest of the FAQ quite helpful as well.

    Best regards,

    Tom
     
    Thomas Tutone, Jul 19, 2006
    #3
  4. xuatla

    xuatla Guest

    Victor Bazarov wrote:
    > xuatla wrote:
    >
    >>When I compile the following test code I got a warning about implicit
    >>typename. This happens in the member functions. Do you know the detail
    >>reason and solution? Thanks.

    >
    >
    > This is covered in the FAQ. Look for "dependent names".
    >
    >
    >>[..]

    >
    >
    > V

    Thank you.

    I read that part ([35.18], [35.19]) but still can't figure out the
    solution for this problem. The one discussed in FAQ is about dependent
    name of member functions, not on the typename. (for member function we
    may change f() to this->f(), but for typename I have no idea and want to
    get help).

    I tried to use cBase<T>::ptr_type, but this is still warned as an
    implicit typename.

    One way I can solve it is to redefine ptr_type in the derived class. But
    this looks not so efficient to me.

    -X
     
    xuatla, Jul 19, 2006
    #4
  5. xuatla

    xuatla Guest

    Thomas Tutone wrote:
    > xuatla wrote:
    >
    >
    >>When I compile the following test code I got a warning about implicit
    >>typename. This happens in the member functions. Do you know the detail
    >>reason and solution? Thanks.
    >>
    >>- X
    >>
    >>----------
    >>
    >>test.cpp:29: warning: `cDerived<T>::ptr_type' is implicitly a typename
    >>test.cpp:29: warning: implicit typename is deprecated, please see the
    >> documentation for details
    >>test.cpp:29: warning: `cDerived<T>::ptr_type' is implicitly a typename
    >>test.cpp:29: warning: implicit typename is deprecated, please see the
    >> documentation for details
    >>
    >>-----------
    >>// -- source code: test.cpp
    >>
    >>#include <iostream>
    >>using namespace std;
    >>
    >>template <typename T>
    >>class cBase
    >>{
    >>public:
    >> typedef unsigned int size_type;
    >> typedef T val_type;
    >> typedef T* ptr_type;
    >>
    >>public:
    >> cBase()
    >> {
    >> // ...
    >> } ;
    >>} ;
    >>
    >>template <class T>
    >>class cDerived : public cBase<T>
    >>{
    >>public:
    >> cDerived()
    >> {
    >> // ...
    >> } ;
    >>//line 28 here
    >> ptr_type getMemory(ptr_type& p) // compiling warning: see above
    >> {
    >> return p;
    >> } ;
    >>} ;
    >>
    >>int main()
    >>{
    >> cDerived<double>::size_type i = 0; // OK
    >> cDerived<double>::val_type d = 0.1; // OK
    >> cDerived<double>::ptr_type p = new double[10]; // OK
    >>
    >> return 1;
    >>}

    >
    >
    > The FAQ answers this:
    >
    > http://www.parashift.com/c -faq-lite/templates.html#faq-35.18
    >
    > I expect you'll find the rest of the FAQ quite helpful as well.
    >
    > Best regards,
    >
    > Tom
    >


    Thanks to Tom. Yes they are very helpful. I read through the whole FAQ
    serveral times before. It seems that not many problems of STL have been
    covered in FAQ so far. The reason I want to use typename is because I
    want to write something close to STL.

    -X
     
    xuatla, Jul 19, 2006
    #5
  6. xuatla wrote:
    > Victor Bazarov wrote:
    >> xuatla wrote:
    >>
    >>> When I compile the following test code I got a warning about
    >>> implicit typename. This happens in the member functions. Do you
    >>> know the detail reason and solution? Thanks.

    >>
    >>
    >> This is covered in the FAQ. Look for "dependent names".
    >>
    >>
    >>> [..]

    >>
    >>
    >> V

    > Thank you.
    >
    > I read that part ([35.18], [35.19]) but still can't figure out the
    > solution for this problem. The one discussed in FAQ is about dependent
    > name of member functions, not on the typename. (for member function we
    > may change f() to this->f(), but for typename I have no idea and want
    > to get help).
    >
    > I tried to use cBase<T>::ptr_type, but this is still warned as an
    > implicit typename.


    Add 'typename' before it. For some reason I thought that the FAQ had
    it. Lemme see... <looking> ...Hmm. Missing. I'll need to contact
    Marshall and see if we can add something.

    > One way I can solve it is to redefine ptr_type in the derived class.


    You don't have to.

    > But this looks not so efficient to me.


    What does efficiency have to do with it?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 19, 2006
    #6
  7. xuatla

    xuatla Guest

    Victor Bazarov wrote:
    > xuatla wrote:
    >
    >>Victor Bazarov wrote:
    >>
    >>>xuatla wrote:
    >>>
    >>>
    >>>>When I compile the following test code I got a warning about
    >>>>implicit typename. This happens in the member functions. Do you
    >>>>know the detail reason and solution? Thanks.
    >>>
    >>>
    >>>This is covered in the FAQ. Look for "dependent names".
    >>>
    >>>
    >>>
    >>>>[..]
    >>>
    >>>
    >>>V

    >>
    >>Thank you.
    >>
    >>I read that part ([35.18], [35.19]) but still can't figure out the
    >>solution for this problem. The one discussed in FAQ is about dependent
    >>name of member functions, not on the typename. (for member function we
    >>may change f() to this->f(), but for typename I have no idea and want
    >>to get help).
    >>
    >>I tried to use cBase<T>::ptr_type, but this is still warned as an
    >>implicit typename.

    >
    >
    > Add 'typename' before it. For some reason I thought that the FAQ had
    > it. Lemme see... <looking> ...Hmm. Missing. I'll need to contact
    > Marshall and see if we can add something.


    Thank you. With typename it works fine now.

    >
    >
    >>One way I can solve it is to redefine ptr_type in the derived class.

    >
    >
    > You don't have to.

    I agree. And I tried to add the following typedef in cDerived, then I
    don't need to specify the scope again.

    public:
    typedef typename cBase<T>::ptr_type ptr_type;

    >
    >
    >>But this looks not so efficient to me.

    >
    >
    > What does efficiency have to do with it?

    I mean the coding efficiency... Sorry, I might be too lazy and greedy
    (but writing "ptr_type" is easier than "typename cBase<T>::ptr_type"... :p)

    >
    > V
     
    xuatla, Jul 19, 2006
    #7
  8. xuatla wrote:
    > Thomas Tutone wrote:
    > > xuatla wrote:
    > >
    > >
    > >>When I compile the following test code I got a warning about implicit
    > >>typename. This happens in the member functions. Do you know the detail
    > >>reason and solution? Thanks.
    > >>
    > >>- X
    > >>
    > >>----------
    > >>
    > >>test.cpp:29: warning: `cDerived<T>::ptr_type' is implicitly a typename
    > >>test.cpp:29: warning: implicit typename is deprecated, please see the
    > >> documentation for details
    > >>test.cpp:29: warning: `cDerived<T>::ptr_type' is implicitly a typename
    > >>test.cpp:29: warning: implicit typename is deprecated, please see the
    > >> documentation for details
    > >>
    > >>-----------
    > >>// -- source code: test.cpp
    > >>
    > >>#include <iostream>
    > >>using namespace std;
    > >>
    > >>template <typename T>
    > >>class cBase
    > >>{
    > >>public:
    > >> typedef unsigned int size_type;
    > >> typedef T val_type;
    > >> typedef T* ptr_type;
    > >>
    > >>public:
    > >> cBase()
    > >> {
    > >> // ...
    > >> } ;
    > >>} ;
    > >>
    > >>template <class T>
    > >>class cDerived : public cBase<T>
    > >>{
    > >>public:
    > >> cDerived()
    > >> {
    > >> // ...
    > >> } ;
    > >>//line 28 here
    > >> ptr_type getMemory(ptr_type& p) // compiling warning: see above
    > >> {
    > >> return p;
    > >> } ;
    > >>} ;
    > >>
    > >>int main()
    > >>{
    > >> cDerived<double>::size_type i = 0; // OK
    > >> cDerived<double>::val_type d = 0.1; // OK
    > >> cDerived<double>::ptr_type p = new double[10]; // OK
    > >>
    > >> return 1;
    > >>}

    > >
    > >
    > > The FAQ answers this:
    > >
    > > http://www.parashift.com/c -faq-lite/templates.html#faq-35.18
    > >
    > > I expect you'll find the rest of the FAQ quite helpful as well.
    > >
    > > Best regards,
    > >
    > > Tom
    > >

    >
    > Thanks to Tom. Yes they are very helpful. I read through the whole FAQ
    > serveral times before. It seems that not many problems of STL have been
    > covered in FAQ so far. The reason I want to use typename is because I
    > want to write something close to STL.



    You're right - it is not as clear as it should be.

    Change line 29 to:

    typename cBase<T>::ptr_type getMemory(typename cBase<T>::ptr_type& p)

    Best regards,

    Tom
     
    Thomas Tutone, Jul 19, 2006
    #8
  9. Here's some code that might help clear it up for you. OTOH, it might
    just make things worse. Good Luck!

    #include <cstddef>
    #include <ostream>
    using namespace std;

    struct
    base_plain
    {
    typedef int type;
    };

    template< typename x >
    struct
    base_tpt
    {
    typedef x type;
    };

    template< typename x >
    struct
    derived_must
    :
    public base_tpt< x >
    {
    // need scope because base_tpt< x > is a dependent type
    // need typename because base_tpt< x >::type is a dependent type
    typedef typename derived_must::type another_type;
    };


    template< typename x >
    struct
    derived_noneed
    :
    public base_plain
    {
    // don't need scope because base_plain is not a dependent type
    // don't need typename because base_plain::type is not
    // a dependent type
    type another_type;

    // don't need scope because base_plain is not a dependent type
    // need typename because base_tpt< x >::type is a dependent type
    typedef typename base_tpt< x >::type yet_another_type;
    };

    int
    main()
    {
    }
     
    Howard Gardner, Jul 20, 2006
    #9
    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. Steve Hill

    implicit typename in template

    Steve Hill, Sep 6, 2003, in forum: C++
    Replies:
    1
    Views:
    464
    ES Kim
    Sep 6, 2003
  2. Marcin Kalicinski

    Implicit typename

    Marcin Kalicinski, Oct 1, 2004, in forum: C++
    Replies:
    1
    Views:
    365
    Mike Wahler
    Oct 1, 2004
  3. =?windows-1252?Q?=8Aimon_T=F3th?=

    Implicit typename warning (g++ linux)

    =?windows-1252?Q?=8Aimon_T=F3th?=, Apr 27, 2005, in forum: C++
    Replies:
    3
    Views:
    1,381
    Victor Bazarov
    Apr 27, 2005
  4. lutorm
    Replies:
    1
    Views:
    2,209
    Rapscallion
    May 21, 2005
  5. Jakob Bieling

    Q: typename or not typename?

    Jakob Bieling, Mar 14, 2006, in forum: C++
    Replies:
    2
    Views:
    377
    Rolf Magnus
    Mar 14, 2006
Loading...

Share This Page