standard compliance typedef-ed code

S

Severin Ecker

hi,

alright, i was directed here from the comp.std.c++ group so I'll discuss
the issue here.
what i learned was that the following code sample results in undefined
behaviour so the outcome that i describe is perfectly legal.

now i would need some help/suggestion for how i can change the code to
defined correct c++ with as little work as possible since the code is
taken from a rather large project.
2 possibilities i can think of would be
a) change the template parameter of the vector to a pointer to Test
b) rearrange the code to move the typedefs after the full definition of
the type (I'm not sure if this even is possible in a lifetime due to
complex cyclic dependencies)


/// post from comp.std.c++


I'm currently working on/with a project and stumbled over a piece of
code which compiled just fine on VC05, VC08, gcc 4.1.1 but not on code
warrior. Then again if i use the stlport stl implementation the code
fails to compile on VC and gcc as well complaining because of the usage
of an undefined type (while compiling the 2nd typedef).


#include <vector>

struct Test;

typedef std::vector<Test> TestVec;
typedef TestVec::const_iterator TestVecIt;


So, actually I'm wondering whether this code complies to the c++
standard (on the one hand i thought templates are only evaluated when
they're instantiated, on the other hand the 2nd typedef needs to know
vector... hmm), but if not if there's any way i can get this to comply
(and compile) without the need to rewrite it using pointers to Test
(aka: std::vector<Test*>)

many thanks in advance!

cheers,
severin
 
A

Alf P. Steinbach

* Severin Ecker:
/// post from comp.std.c++


I'm currently working on/with a project and stumbled over a piece of
code which compiled just fine on VC05, VC08, gcc 4.1.1 but not on code
warrior. Then again if i use the stlport stl implementation the code
fails to compile on VC and gcc as well complaining because of the usage
of an undefined type (while compiling the 2nd typedef).


#include <vector>

struct Test;

Replace with

#include "Test.h"

typedef std::vector<Test> TestVec;
typedef TestVec::const_iterator TestVecIt;

Now OK :)

So, actually I'm wondering whether this code complies to the c++
standard

Nope, as noted over in comp.std.c++; standard library container element types
cannot be incomplete.

(on the one hand i thought templates are only evaluated when
they're instantiated, on the other hand the 2nd typedef needs to know
vector... hmm), but if not if there's any way i can get this to comply
(and compile) without the need to rewrite it using pointers to Test
(aka: std::vector<Test*>)

See above.

It's possible that that will drag in some other problem, but then, that's then
likely more in the direction of the real problem.


Cheers & hth.,

- Alf
 
S

Severin Ecker

Alf said:
* Severin Ecker:

Replace with

#include "Test.h"



Now OK :)

unfortunately it's not that easy.
the real code consists of about 8-10 klasses each of which is used in
typedefs like the ones above. and for the greatness of it, these classes
use some of the typedefs of other classes in their own definition ;/

Nope, as noted over in comp.std.c++; standard library container element
types cannot be incomplete.



See above.

It's possible that that will drag in some other problem, but then,
that's then likely more in the direction of the real problem.


Cheers & hth.,

- Alf

seems like i really have to go the long way but in any case thanks for
the clarification of the code flaw.


cheers,
severin
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top