compile error? why?

D

Danny Boelens

Hi all,

today I ran into a compile error after a compiler upgrade. I made a small
example to demonstrate my compile error:

template<typename T1, typename T2>
class A
{};

class B
{
static void f( const A<int,int>& inA = A<int,int>() )
{}
};

int main()
{
return 0;
}

This code compiles fine with Metrowerks CodeWarrior 8.3 (and also with
Microsoft Visual Studio 6 SP5 and the online "try it out" Comeau C++
compiler). After an upgrade to Metrowerks CodeWarrior 9.4, I faced a
compiler error. I tried to compile the code above with several versions of
gcc (2.95.3, 3.3.something and the new 4.0.0) and these give basically the
same compiler error as CW 9.4:
main.cpp:8: parse error before '>'
(line 8 is the line with the static function f and default argument)

Note that this is only a simplified example and the actual class B also
contains a bunch of members/member functions, class A actually uses T1 and
T2 etc...

I'm far from a C/C++ expert, and I still didn't figure out if this code
should compile or not (and why exactly it is not valid C++ in that case).
However, I noticed that replacing 'class B' with 'namespace B' -- at the
same time removing the ';' after the '}' of course -- fixes the compile
error in both gcc and CW9.4. So I'm still wondering what is going on here...

Thanks in advance for any help!

Best regards,
Danny Boelens
 
C

CrayzeeWulf

Danny said:
template<typename T1, typename T2>
class A
{};

class B
{
static void f( const A<int,int>& inA = A<int,int>() )
{}
};

int main()
{
return 0;
}

The following appears to work (under g++-3.3.5), still looking for an
explanation:


template<typename T1, typename T2>
class A
{
} ;


class B
{
public:
typedef A<int,int> MyAType ;
//static void f( const A<int,int>& inA = A<int,int>() )
static void f( const MyAType& inA = MyAType() )
{}
};


Thanks,
 

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
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top