typedefs and templates

K

kf3cr

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;}
 
V

Victor Bazarov

kf3cr said:
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 said:
}
};

int main(void) {return 0;}


V
 
K

kf3cr

Victor said:
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.
'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.
 
M

Michael Doubez

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







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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top