On Tuesday 12 May 2009, Andreas Buykx wrote: > Hi all, > > Yesterday I ran into a g++ (3.4.6) compiler problem for code that I have > been compiling without a problem using the Intel (9.0) compiler. Here's a > code snippet that shows what happened: > > template<typename A, typename B> > class Foo { }; > > struct Bar { > void method ( Foo<int,int> const& stuff = Foo<int,int>() ); > }; > The following compiles with g++-4.2.3, see the extra parentheses -------------------------------------------------- template< typename A, typename B> class Foo {}; struct Bar { void method( Foo<int,int> const& stuff = (Foo<int,int>()) ); }; -------------------------------------------------- Though I don't know the exact differences > The g++ compiler error is: > > foo.cpp:5: error: expected `,' or `...' before '>' token > foo.cpp:5: error: wrong number of template arguments (1, should be 2) > foo.cpp:2: error: provided for `template<class A, class B> struct Foo' > foo.cpp:5: error: default argument missing for parameter 2 of `void > Bar::method(const Foo<int, int>&, int)' > > Apparently, the default argument is not accepted when written this way, > and the compiler assumes that instead of the second template argument a > new function argument is specified, for which it then expects a default > value because the `stuff` argument has one. I can help the compiler by > creating a typedef, and then everything compiles fine: > > template<typename A, typename B> > class Foo { }; > > struct Bar { > typedef Foo<int,int> FooType; > void method ( FooType const& stuff = FooType() ); > }; > > So I can solve my problem, but I don't understand what is going on. Do I > miss a C++ (template?) language feature here and am I doing something > wrong, or is the g++ compiler wrong in not accepting the first piece of > code? > > Note BTW that this also compiles ... > > template<typename A, typename B> > class Foo { }; > > void method ( Foo<int,int> const& stuff = Foo<int,int>() ); > > >From stackoverflow, where I posted the exact same question, two other > > people confirmed that they could compile the code with two other > compilers: IBM's xlC V7.0 and Comeau.