Subtypes of templated types (in templated functions)

Discussion in 'C++' started by Marijn, Feb 12, 2004.

  1. Marijn

    Marijn Guest

    I recently told g++ (3.2) something to the effect of:

    template <class T>
    struct test{
    typedef int type;
    };

    template <class T>
    void f(){
    test<T>::type i;
    }

    To which g++ responded (in an unfriendly manner):

    test.cpp: In function 'void f()':
    test.cpp:11: warning: 'typename test<T>::type' is implicitly a
    typename
    test.cpp:11: warning: implicit typename is deprecated, please see the
    documentation for details

    Which confused me quite a lot. "test<T>::type" look explicit enough to
    me... it only seems to happen when both the class and the function are
    templates, and I can declare a test<T> allright, just not a
    test<T>::type. Is this a language problem? I do not have any other
    compilers so I do not know whether it could be a compiler thingy.
    Suffice to say, it annoys me, I was trying to use a std::vector's
    iterators in a templated container class that used an internal vector,
    and it just would not allow it.

    Marijn Haverbeke
     
    Marijn, Feb 12, 2004
    #1
    1. Advertising


  2. > template <class T>
    > void f(){
    > test<T>::type i;
    > }
    >
    > To which g++ responded (in an unfriendly manner):
    >
    > test.cpp: In function 'void f()':
    > test.cpp:11: warning: 'typename test<T>::type' is implicitly a
    > typename
    > test.cpp:11: warning: implicit typename is deprecated, please see the
    > documentation for details
    >

    Just a guess but probably the compiler simply tries to tell you that you
    should write
    typename test<T>::type i;
     
    Patrik Stellmann, Feb 12, 2004
    #2
    1. Advertising

  3. "Marijn" <> wrote in message
    news:...
    > I recently told g++ (3.2) something to the effect of:
    >
    > template <class T>
    > struct test{
    > typedef int type;
    > };
    >
    > template <class T>
    > void f(){
    > test<T>::type i;
    > }
    >
    > To which g++ responded (in an unfriendly manner):
    >
    > test.cpp: In function 'void f()':
    > test.cpp:11: warning: 'typename test<T>::type' is implicitly a
    > typename
    > test.cpp:11: warning: implicit typename is deprecated, please see the
    > documentation for details
    >
    > Which confused me quite a lot. "test<T>::type" look explicit enough to
    > me... it only seems to happen when both the class and the function are
    > templates, and I can declare a test<T> allright, just not a
    > test<T>::type.


    The compiler cannot know that test<T>::type is a typename and not a static
    data member when it is compiling f(), since at that point it does not know
    the type of T, and it does not know what template specialisations of test<T>
    might exist. Or something like that, any corrections or clarifications
    welcome.

    > Is this a language problem?


    It a requirement of the language that you say

    typename test<T>::type i;

    in this situation. Its arguably a language problem since it seems this
    ambiguity (and a few others) were not appreciated when templates where added
    informally to the language, typename got added later when this became known.

    > I do not have any other
    > compilers so I do not know whether it could be a compiler thingy.
    > Suffice to say, it annoys me, I was trying to use a std::vector's
    > iterators in a templated container class that used an internal vector,
    > and it just would not allow it.


    All you have to do is add typename, in the appropriate places.

    >
    > Marijn Haverbeke


    john
     
    John Harrison, Feb 12, 2004
    #3
  4. Well, I was thought that for templates, it makes sense to explicitly
    specify, wheter something is to be considered as a type or a non-type.
    Maybe the following example is helpful:


    struct foo{
    typedef int t;
    };

    struct bar{
    static const int t=10;
    };

    int m = 10;

    template <typename T> void f(){
    T::t* m; // declaration of an int* (for foo),
    // or multiplication (for bar)
    }

    int main(){
    f<foo>();
    f<bar>();
    }
     
    Christian Jaeger, Feb 12, 2004
    #4
  5. Marijn

    Sharad Kala Guest

    "Marijn" <> wrote in message
    news:...
    > I recently told g++ (3.2) something to the effect of:
    >
    > template <class T>
    > struct test{
    > typedef int type;
    > };
    >
    > template <class T>
    > void f(){
    > test<T>::type i;
    > }
    >
    > To which g++ responded (in an unfriendly manner):
    >
    > test.cpp: In function 'void f()':
    > test.cpp:11: warning: 'typename test<T>::type' is implicitly a
    > typename
    > test.cpp:11: warning: implicit typename is deprecated, please see the
    > documentation for details
    >
    > Which confused me quite a lot. "test<T>::type" look explicit enough to
    > me... it only seems to happen when both the class and the function are
    > templates, and I can declare a test<T> allright, just not a
    > test<T>::type. Is this a language problem? I do not have any other
    > compilers so I do not know whether it could be a compiler thingy.
    > Suffice to say, it annoys me, I was trying to use a std::vector's
    > iterators in a templated container class that used an internal vector,
    > and it just would not allow it.


    Consider test<T>::type* i;
    May be your intention was to declare i as a pointer but compiler could take it
    for a multiplication.
    Hence the need of typename is needed in case of dependent names to tell that
    what follows is a type.
    Josuttis/Vandevoorde cover dependent names etc in good detail in Part II of
    their book "C++ Templates".

    Also it's worth to read what Herb Sutter has to say in this article -
    http://www.gotw.ca/gotw/035.htm

    -Sharad
     
    Sharad Kala, Feb 12, 2004
    #5
  6. Marijn

    Marijn Guest

    > All you have to do is add typename, in the appropriate places.

    Ahh okay. Thanks for the clarification. I think C++ is the only
    language that keeps surprising me after 4 years of using it.

    Marijn Haverbeke
     
    Marijn, Feb 13, 2004
    #6
    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. Daniel Hagen
    Replies:
    3
    Views:
    719
    Oscar kind
    Jul 31, 2004
  2. Crutcher
    Replies:
    1
    Views:
    369
    Alex Martelli
    Dec 31, 2005
  3. Tony Johansson

    LSP and subtypes

    Tony Johansson, Oct 14, 2005, in forum: C++
    Replies:
    1
    Views:
    325
    Regulus
    Oct 14, 2005
  4. VHDL Types/Subtypes

    , Mar 8, 2007, in forum: VHDL
    Replies:
    8
    Views:
    779
    Jonathan Bromley
    Mar 14, 2007
  5. tutmann
    Replies:
    4
    Views:
    507
    Victor Bazarov
    Jun 18, 2010
Loading...

Share This Page