compilation error with default argument

S

subramanian100in

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
 
B

Balog Pal

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).
 
J

James Kanze

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.
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top