Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

compilation error with default argument

5 views
Skip to first unread message

subramanian100in@yahoo.com, India

unread,
Dec 24, 2009, 10:06:04 AM12/24/09
to
This question is for learning purpose only.
Consider the program x.cpp:

#include <cstdlib>

using namespace std;

void fn(int x = 7);
void fn(int = 7);

int main()
{
return EXIT_SUCCESS;
}

When I compiled this program with g++3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp,
I get the following compilation error:

x.cpp:6: error: default argument given for parameter 1 of `void fn
(int)'
x.cpp:5: error: after previous specification in `void fn(int)'

But Stroustrup in his book "The C++ Programming Language - Third
Edition(NOT the Special Third Edition)" has mentioned the following in
page 153 in section "7.5 Default Arguments":
A default argument can be repeated in a susequent declaration in the
same scope but not changed. Having this being stated, I do not
understand why the compiler is giving error. Have I misunderstood what
Sttroustrup has stated ? Kindly clarify.

Thanks
V.Subramanian

Balog Pal

unread,
Dec 24, 2009, 10:36:55 AM12/24/09
to

<subraman...@yahoo.com>

> void fn(int x = 7);
> void fn(int = 7);

> When I compiled this program with g++3.4.3 as


> g++ -std=c++98 -pedantic -Wall -Wextra x.cpp,
> I get the following compilation error:
>
> x.cpp:6: error: default argument given for parameter 1 of `void fn
> (int)'
> x.cpp:5: error: after previous specification in `void fn(int)'
>
> But Stroustrup in his book "The C++ Programming Language - Third
> Edition(NOT the Special Third Edition)" has mentioned the following in
> page 153 in section "7.5 Default Arguments":
> A default argument can be repeated in a susequent declaration in the
> same scope but not changed. Having this being stated, I do not
> understand why the compiler is giving error. Have I misunderstood what
> Sttroustrup has stated ? Kindly clarify.

Dunno what BS meant, the standard doesn't allow redefining the same default
param even to the same value (see example in 3.8.6p4 last words). Though
you can make further params default.

quote 3.8.6p4:

For nontemplate functions, default arguments can be added in later
declarations of a function in the same scope. Declarations in different
scopes have completely distinct sets of default arguments. That is,
declarations in inner scopes do not acquire default arguments from
declarations in outer scopes, and vice versa. In a given function
declaration, all parameters subsequent to a parameter with a default
argument shall have default arguments supplied in this or previous
declarations. A default argument shall not be redefined by a later
declaration (not even to the same value).


James Kanze

unread,
Dec 25, 2009, 7:48:15 AM12/25/09
to
On Dec 24, 3:36 pm, "Balog Pal" <p...@lib.hu> wrote:
> <subramanian10...@yahoo.com>

> > void fn(int x = 7);
> > void fn(int = 7);
> > When I compiled this program with g++3.4.3 as
> > g++ -std=c++98 -pedantic -Wall -Wextra x.cpp,
> > I get the following compilation error:

> > x.cpp:6: error: default argument given for parameter 1 of `void fn
> > (int)'
> > x.cpp:5: error: after previous specification in `void fn(int)'

> > But Stroustrup in his book "The C++ Programming Language - Third
> > Edition(NOT the Special Third Edition)" has mentioned the following in
> > page 153 in section "7.5 Default Arguments":
> > A default argument can be repeated in a susequent declaration in the
> > same scope but not changed. Having this being stated, I do not
> > understand why the compiler is giving error. Have I misunderstood what
> > Sttroustrup has stated ? Kindly clarify.

> Dunno what BS meant, the standard doesn't allow redefining the
> same default param even to the same value (see example in
> 3.8.6p4 last words).

But IIRC, some earlier drafts did allow it. And the text of
"The C++ Programming Language" predates the final standard, and
may be based on one of those earlier drafts.

--
James Kanze

0 new messages