Q: typename or not typename?

Discussion in 'C++' started by Jakob Bieling, Mar 14, 2006.

  1. Hi,

    I recently switched compilers (VC++7.1 to VC++8) and it started
    complaining about my use of 'typename' in template classes. So far, I
    have never fully understood where I need to put 'typename' and where
    not. Please consider the following snippet:

    #include <list>

    template <typename T>
    struct test_case
    {
    test_case (/* (1) */T::iterator i = (/* (2) */T::iterator ())
    {
    (/* (3) */T::iterator ii;
    }
    };

    int main (int argc, char* argv [])
    {
    test_case <std::list <int> > foo;
    }

    As you can see, I marked three spots. In (1), the compiler requires
    me to put 'typename'. In (2) I am not allowed to. In (3) it compiles
    with or without.

    Why those differences? And what is the reasoning behind requiring it
    at all?

    Thanks for your time!
    --
    jb

    (reply address in rot13, unscramble first)
     
    Jakob Bieling, Mar 14, 2006
    #1
    1. Advertising

  2. Jakob Bieling wrote:
    > I recently switched compilers (VC++7.1 to VC++8) and it started
    > complaining about my use of 'typename' in template classes. So far, I
    > have never fully understood where I need to put 'typename' and where
    > not. Please consider the following snippet:
    >
    > #include <list>
    >
    > template <typename T>
    > struct test_case
    > {
    > test_case (/* (1) */T::iterator i = (/* (2) */T::iterator ())
    > {
    > (/* (3) */T::iterator ii;


    (1): yes, (2): no, (3): yes.

    > }
    > };
    >
    > int main (int argc, char* argv [])
    > {
    > test_case <std::list <int> > foo;
    > }
    >
    > As you can see, I marked three spots. In (1), the compiler requires
    > me to put 'typename'. In (2) I am not allowed to. In (3) it compiles
    > with or without.
    >
    > Why those differences? And what is the reasoning behind requiring it
    > at all?


    It's required for (1) because you need to help your compiler to understand
    that what it's compiling is a declaration (same with (3)). However, since
    (2) is the expression, 'typename' has to be omitted.

    Imagine that you try to instantiate your 'test_case' template with a class
    that has 'iterator' as a function or a data member? _You_ know that it is
    not in 'std::list<int>' but when the compiler first looks at the template,
    it has no way to know.

    V
    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, Mar 14, 2006
    #2
    1. Advertising

  3. Jakob Bieling

    Rolf Magnus Guest

    Jakob Bieling wrote:

    > Hi,
    >
    > I recently switched compilers (VC++7.1 to VC++8) and it started
    > complaining about my use of 'typename' in template classes. So far, I
    > have never fully understood where I need to put 'typename' and where
    > not.


    You need it if the type depends on a template argument.

    > Please consider the following snippet:
    >
    > #include <list>
    >
    > template <typename T>
    > struct test_case
    > {
    > test_case (/* (1) */T::iterator i = (/* (2) */T::iterator ())
    > {
    > (/* (3) */T::iterator ii;
    > }
    > };
    >
    > int main (int argc, char* argv [])
    > {
    > test_case <std::list <int> > foo;
    > }
    >
    > As you can see, I marked three spots. In (1), the compiler requires
    > me to put 'typename'. In (2) I am not allowed to. In (3) it compiles
    > with or without.


    Hmm, I would have expected that it's needed in all three places. My compiler
    (g++) requires it in (1) and (3) and doesn't care in (2).

    > Why those differences? And what is the reasoning behind requiring it
    > at all?


    Because in some situations, it's quite some extra work for the compiler to
    find out whether T::iterator is the name of a type or a variable, since it
    doesn't know anything about T yet when parsing the template. To reduce
    context dependancies, 'typename' is used to tell the compiler that the name
    denotes a type.
     
    Rolf Magnus, Mar 14, 2006
    #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. Replies:
    1
    Views:
    778
  2. Bryce Fischer
    Replies:
    0
    Views:
    2,308
    Bryce Fischer
    Jan 3, 2007
  3. Replies:
    6
    Views:
    622
    Victor Bazarov
    Oct 20, 2005
  4. lizhuo
    Replies:
    4
    Views:
    299
    Alf P. Steinbach
    Dec 12, 2006
  5. Sanil
    Replies:
    2
    Views:
    4,767
    James Kanze
    Dec 14, 2007
Loading...

Share This Page