Correct use of templated class'es inner structs/classes?

T

Tomas Ukkonen

Hi

I'm currently trying to make my old code to compile with
new gcc 3.4.0. It compiles correctly in 3.3.3 so I'm not
sure if this is compiler bug or not.


Code very close to a example below fails to compile
because gcc 3.4.0 complains:
"error: expected a type got 'Y<T>::particle'"
at Y<T>::func()'s iterator declaration.


EXAMPLE
(I actually haven't tried to get this compile).
-----------------------------------

example.h:
template <typename T>
class Y{
public:

struct particle {
OtherClass<T> data;
};

void func();

std::vector<Y<T>::particle> list;
}


example.cpp:
template <typename T>
void Y<T>::func(){
typename std::vector<Y<T>::particle>::iterator i;

i = list.begin();

while(i != list.end()){
// do something
}
}
--------------------------



TIA,
Tomas Ukkonen
 
R

Rob Williscroft

Tomas Ukkonen wrote in in
comp.lang.c++:
Hi

I'm currently trying to make my old code to compile with
new gcc 3.4.0. It compiles correctly in 3.3.3 so I'm not
sure if this is compiler bug or not.


Code very close to a example below fails to compile
because gcc 3.4.0 complains:
"error: expected a type got 'Y<T>::particle'"
at Y<T>::func()'s iterator declaration.


EXAMPLE
(I actually haven't tried to get this compile).

Why not ?.
-----------------------------------

example.h:
template <typename T>
class Y{
public:

struct particle {
OtherClass<T> data;

T data; // to make a compilable example !
};

void func();

std::vector<Y<T>::particle> list;

std::vector< typename Y<T>::particle > list;

However Y (without the <T>) is an injected class name so you should
be able to do this:

std::vector< Y::particle > list;

However of the compilers I tried on CBuilderX previeiw (EDG based)
handled it, g++ 3.4 (prereleas) didn't, your version maybe later than
mine so try it.


missing ;
example.cpp:

This should be in a header file.
template <typename T>
void Y<T>::func(){
typename std::vector< Y<T>::particle >::iterator i;


i = list.begin();

while(i != list.end()){
// do something
}
}

missing ;


The typename is required as Y<T> is dependant on a template argument
so particle in Y<T>::particle is a dependant-name, you need to tell
the compiler its a type, otherwise it assumes its a non-type.

g++ 3.3.x didn't require this as it didn't support 2-phase name
lookup, as its supposed to. i.e. 3.4 is correct.

HTH.

Rob.
 
M

Michiel Salters

Tomas Ukkonen said:
Code very close to a example below fails to compile
because gcc 3.4.0 complains:
"error: expected a type got 'Y<T>::particle'"
at Y<T>::func()'s iterator declaration.

Indeed. Without knowing T, gcc cannot tell whether particle is
a member type, a data member, a member function, or a member template.

The solution is (obviously) to tell the compiler:

std::vector<typename Y<T>::particle> list;

Regards,
Michiel Salters
 
T

Tomas Ukkonen

Indeed. Without knowing T, gcc cannot tell whether particle is
a member type, a data member, a member function, or a member template.

The solution is (obviously) to tell the compiler:

std::vector<typename Y<T>::particle> list;

Thanks. I managed to get gcc to accept something similar
while waiting for reply. I have never really read how
templates should work according to standard so I wasn't
sure what the correct behaviour of gcc should be.
 
T

Tomas Ukkonen

Tomas Ukkonen wrote in in
comp.lang.c++:
Why not ?.

Guess I'm lazy. It was just pseudocode for
illustrating the problem.
std::vector< typename Y<T>::particle > list;

However Y (without the <T>) is an injected class name so you should
be able to do this:

std::vector< Y::particle > list;

However of the compilers I tried on CBuilderX previeiw (EDG based)
handled it, g++ 3.4 (prereleas) didn't, your version maybe later than
mine so try it.


Thanks for help. It seems I started asking for help a bit too
quickly as I managed to get the code working myself. I don't have
time to test the latter solution right now.
This should be in a header file.

Yes, but I usually code in a style where I either put templated code to
cpp files as normally and then include it to header file or
use explicit template instantations.
 

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

Forum statistics

Threads
474,169
Messages
2,570,918
Members
47,458
Latest member
Chris#

Latest Threads

Top