This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: GCC-3.1: g++ and default parameters of methods


Rene Rebe <rene.rebe@gmx.net> writes:

> Hi.
> 
> During converting GSMP to compile with the recent g++ 3.1 we had to
> convert many occurencies of this style:
> 
> xyz.hh:
> void some_func (int p1, int p2 = 666);
> 
> xyz:cc:
> void some_func (int p1, int p2 = 666);
> 
> to xyz.cc:
> void some_func (int p1, int p2);
>                               ^^^^^^^^
> 
> Because g++ emmited (e.g.) the following error message:
> 
> DataLayer.cc:224: default argument given for parameter 4 of `
>    GSML::DataLayer::DataLayer(const std::string&, GSML::DataFileIndex*, 
>    GSML::PCMDatatype*, unsigned int = 44100)'
> ../include/DataLayer.hh:113: after previous specification in `
>    GSML::DataLayer::DataLayer(const std::string&, GSML::DataFileIndex*, 
>    GSML::PCMDatatype*, unsigned int = 44100)'
> make[2]: *** [DataLayer.o] Error 1
> 
> Is this a really enforced by the ANSI C++ Standard and so a feature,
> or a regression?

This is mandated by the standard. See 8.3.6.4:

-- Quote
[...] A default argument shall not be redefined by a later declaration
(not even to the same value).
-- End quote

Just for your amusement:

xxx.cpp
void foo(int x = 10);

foo(); // calls foo(10)

yyy.h
void foo(int x = 0);

foo(); // calls foo(0)

This hints that you should define your default arguments in a header
file and #include it whenever you want to use the function, that is,
never declare the function in a cpp file to save an #include, unless
you like subtle bugs and maintenance nightmares.

-- 
Oscar


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]