typedefs and templates

Discussion in 'C++' started by kf3cr, Oct 5, 2009.

  1. kf3cr

    kf3cr Guest

    I am including the code that does not compile below. I am curious as to
    why this does not compile, and I would appreciate it if anybody knew
    where in the c++ standard I could info to better understand this issue.

    For the sake of this post I am going to assume that gcc 4.2.1 is better
    with the c++ standard than my copy of visual studio 2009 version
    9.0.30729.1 SP since gcc gives the error and visual studio compiles it.

    gcc Error text:
    main.cpp: In member function 'void B<G>::f()':
    main.cpp:22: error: expected ';' before 'itr2'


    #include <vector>

    template <typename T>
    class A
    {
    public:
    typedef std::vector<int> stuffs;
    typedef stuffs::const_iterator ConstIterator;
    };


    template <typename G>
    class B
    {

    public:
    void f(void)
    {
    A<bool>::ConstIterator itr1; // this is ok

    A<G>::ConstIterator itr2; // Problem here
    }
    };

    int main(void) {return 0;}
     
    kf3cr, Oct 5, 2009
    #1
    1. Advertising

  2. kf3cr wrote:
    > I am including the code that does not compile below. I am curious as to
    > why this does not compile, and I would appreciate it if anybody knew
    > where in the c++ standard I could info to better understand this issue.
    >
    > For the sake of this post I am going to assume that gcc 4.2.1 is better
    > with the c++ standard than my copy of visual studio 2009 version
    > 9.0.30729.1 SP since gcc gives the error and visual studio compiles it.
    >
    > gcc Error text:
    > main.cpp: In member function 'void B<G>::f()':
    > main.cpp:22: error: expected ';' before 'itr2'
    >
    >
    > #include <vector>
    >
    > template <typename T>
    > class A
    > {
    > public:
    > typedef std::vector<int> stuffs;
    > typedef stuffs::const_iterator ConstIterator;
    > };
    >
    >
    > template <typename G>
    > class B
    > {
    >
    > public:
    > void f(void)


    C-ism: omit the 'void' when defining an empty parameter list. Don't put
    anything where nothing is expected. Just write

    void f()

    While it's not an error, it clutters the code with something that is
    totally unnecessary.

    > {
    > A<bool>::ConstIterator itr1; // this is ok
    >
    > A<G>::ConstIterator itr2; // Problem here


    'ConstIterator' is a dependent name. Turn up the warning level on VC++
    or disable the language extensions, and you may see the problem.

    The solution is to use 'typename' before the type name:

    typename A<G>::ConstIterator itr2;

    > }
    > };
    >
    > int main(void) {return 0;}



    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, Oct 5, 2009
    #2
    1. Advertising

  3. kf3cr

    kf3cr Guest

    Victor Bazarov wrote:
    > kf3cr wrote:
    >> I am including the code that does not compile below. I am curious as to
    >> why this does not compile, and I would appreciate it if anybody knew
    >> where in the c++ standard I could info to better understand this issue.
    >>
    >> For the sake of this post I am going to assume that gcc 4.2.1 is better
    >> with the c++ standard than my copy of visual studio 2009 version
    >> 9.0.30729.1 SP since gcc gives the error and visual studio compiles it.
    >>
    >> gcc Error text:
    >> main.cpp: In member function 'void B<G>::f()':
    >> main.cpp:22: error: expected ';' before 'itr2'
    >>
    >>
    >> #include <vector>
    >>
    >> template <typename T>
    >> class A
    >> {
    >> public:
    >> typedef std::vector<int> stuffs;
    >> typedef stuffs::const_iterator ConstIterator;
    >> };
    >>
    >>
    >> template <typename G>
    >> class B
    >> {
    >>
    >> public:
    >> void f(void)

    >
    > C-ism: omit the 'void' when defining an empty parameter list. Don't put
    > anything where nothing is expected. Just write
    >
    > void f()
    >
    > While it's not an error, it clutters the code with something that is
    > totally unnecessary.


    oops, I always forget that, and somebody always points it out. I don't
    think I will ever break that.

    >
    >> {
    >> A<bool>::ConstIterator itr1; // this is ok
    >>
    >> A<G>::ConstIterator itr2; // Problem here

    >
    > 'ConstIterator' is a dependent name. Turn up the warning level on VC++
    > or disable the language extensions, and you may see the problem.
    >
    > The solution is to use 'typename' before the type name:
    >
    > typename A<G>::ConstIterator itr2;


    HA! When in doubt, add typename. Thanks so much for helping. I can't
    believe I missed this. Every other line seems to need an extra typename
    or two, and I usually catch these errors. Thanks again.

    >
    >> }
    >> };
    >>
    >> int main(void) {return 0;}

    >
    >
    > V
     
    kf3cr, Oct 5, 2009
    #3
  4. On 5 oct, 22:11, kf3cr <> wrote:
    > Victor Bazarov wrote:
    > > kf3cr wrote:
    > >> I am including the code that does not compile below.  I am curious as to
    > >> why this does not compile, and I would appreciate it if anybody knew
    > >> where in the c++ standard I could info to better understand this issue..

    >
    > >> For the sake of this post I am going to assume that gcc 4.2.1 is better
    > >> with the c++ standard than my copy of visual studio 2009 version
    > >> 9.0.30729.1 SP since gcc gives the error and visual studio compiles it..

    >
    > >> gcc Error text:
    > >> main.cpp: In member function 'void B<G>::f()':
    > >> main.cpp:22: error: expected ';' before 'itr2'

    >
    > >> #include <vector>

    >
    > >> template <typename T>
    > >> class A
    > >> {
    > >>     public:
    > >>         typedef std::vector<int> stuffs;
    > >>         typedef stuffs::const_iterator ConstIterator;
    > >> };

    >
    > >> template <typename G>
    > >> class B
    > >> {

    >
    > >>     public:
    > >>         void f(void)

    >
    > > C-ism: omit the 'void' when defining an empty parameter list.  Don't put
    > > anything where nothing is expected.  Just write

    >
    > >                 void f()

    >
    > > While it's not an error, it clutters the code with something that is
    > > totally unnecessary.

    >
    > oops, I always forget that, and somebody always points it out.  I don't
    > think I will ever break that.
    >
    >
    >
    > >>         {
    > >>             A<bool>::ConstIterator itr1; // this is ok

    >
    > >>             A<G>::ConstIterator itr2; // Problem here

    >
    > > 'ConstIterator' is a dependent name.  Turn up the warning level on VC++
    > > or disable the language extensions, and you may see the problem.

    >
    > > The solution is to use 'typename' before the type name:

    >
    > >                     typename A<G>::ConstIterator itr2;

    >
    > HA! When in doubt, add typename.  Thanks so much for helping.  I can't
    > believe I missed this.  Every other line seems to need an extra typename
    > or two, and I usually catch these errors.  Thanks again.


    Well. With the current standard, putting a typename where it is not
    needed is an error.

    IIRC the next standard will release that restriction and you will be
    able to happily litter your code with 'typename' all over the
    source :)

    --
    Michael
     
    Michael Doubez, Oct 6, 2009
    #4
    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. dwrayment

    Templates and Typedefs

    dwrayment, Aug 12, 2003, in forum: C++
    Replies:
    6
    Views:
    402
    Sam Holden
    Aug 14, 2003
  2. JKop
    Replies:
    3
    Views:
    483
  3. Noah Roberts
    Replies:
    7
    Views:
    683
    Bo Persson
    May 3, 2006
  4. recover
    Replies:
    2
    Views:
    819
    recover
    Jul 25, 2006
  5. Replies:
    1
    Views:
    381
    Victor Bazarov
    Jan 25, 2008
Loading...

Share This Page