Templates & Typdefs with g++ v4.0.0

P

Pete

In moving from gcc 3.4 to v4.0.0 my code stops working (what a
surprise).

Basically, I have a header file:
____________________
template<int I, int J>
class Inner
{
Inner(){};
};

class Outer
{
Outer(){};
typedef Inner<1,2> Foo;
};
---------------
and a source file:
___________________
#include "test.h"
int main(void)
{
};
template class Outer::Foo;
-------------------
which, when compiling, gives the errors

test.cpp:5: error: using typedef-name 'Outer::Foo' after 'class'
test.h:10: error: 'Outer::Foo' has a previous declaration here

VS.NET gives the error "C2242: typedef name cannot follow
class/struct/union"

I'm a bit stumped, and I don't know templates very well. The source
file code had originally read:

.....
template Outer::Foo;

which compiles under VS.NET, but gives the G++ error:

test.cpp:8: error: expected unqualified-id before ';' token

Unfortunately I am in the position that I need it to compile under both
OSs.
 
G

Gernot Frisch

Pete said:
In moving from gcc 3.4 to v4.0.0 my code stops working (what a
surprise).

Basically, I have a header file:
____________________
template<int I, int J>
class Inner
{
Inner(){};
};

class Outer
{
Outer(){};
typedef Inner<1,2> Foo;
};
---------------
and a source file:
___________________
#include "test.h"
int main(void)
{
};
template class Outer::Foo;

you don't refer to this line at all, why don't you just dump it? The
declaration of the "Outer" class including the "foo" _is_ done in the
header file.
 
P

Pete

In simplifying the problem, I may have oversimplified the (100K lines
of code) file structure to reproduce the error messages :)

If the offending line is commented out I get a whole mass of nasties
popping up in other places (in g++, but not VS.NET!).

Just noticed that my start version of gcc was 3.2.2, not 3.4
 
G

Gernot Frisch

Pete said:
In simplifying the problem, I may have oversimplified the (100K
lines
of code) file structure to reproduce the error messages :)

If the offending line is commented out I get a whole mass of nasties
popping up in other places (in g++, but not VS.NET!).

Just noticed that my start version of gcc was 3.2.2, not 3.4

So.. post these instead. Because what you do in the header is
sufficient for declaration.
-Gernot
 
R

Rob Williscroft

Gernot Frisch wrote in in
comp.lang.c++:
So.. post these instead. Because what you do in the header is
sufficient for declaration.

Declaration isn't the point, the line:

template class Outer::Foo;

Is an attempt to explicitly instantiate the class Inner<1, 2>.

Writeing:

template class Inner< 1, 2 >;

should satisfy both g++ and MSVC.

From a brief reading of 14.7.2 Explicit instantiation, it seems that
the Standard doesn't require the use of 'class', but it does require
that the /declaraion/ following 'template' be either a /template-id/
or a /template-name/ a typedef is *never* one of those, so the only
portable solution is to use the replacment above.

Rob.
 

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,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top