default arguments

R

Rainer

void f(int a=0,int b=1,int c=2){}
f(); //OK
f(1); //OK
f(1,2); //OK
f(1,2,3); //OK
f(,2,3)// NOT OK

f(1,,3); won´t work either, because that´s IMHO the way default params are
used in the C++ standard. you only can ommit the trailing params.

HTH
Rainer
 
A

Agent Mulder

Hi group,
I want to know why this doesn't work

void f(int a=0,int b=1,int c=2){}
int main()
{
f(); //OK
f(1); //OK
f(1,2); //OK
f(1,2,3); //OK
f(,2,3)// NOT OK
return 0;
}

-X
 
A

Arbesto Pelta

Hi group,
I want to know why this doesn't work

void f(int a=0,int b=1,int c=2){}
int main()
{
f(); //OK
f(1); //OK
f(1,2); //OK
f(1,2,3); //OK
f(,2,3)// NOT OK
return 0;
}

-X

I think that you need read a C++ book or tutorial. I recommend it because
IMHO C++ is not a languaje to learn simply using it.
 
T

Tom

Agent Mulder said:
I want to know why this doesn't work

void f(int a=0,int b=1,int c=2){}
int main()
{
f(); //OK
f(1); //OK
f(1,2); //OK
f(1,2,3); //OK
f(,2,3)// NOT OK
return 0;
}

As others have said, what you've written doesn't work because it's not
C++. As to "why?," Stroustrup devotes several pages in his "The
Design and Evolution of C++" to (i) a proposal to incorporate the kind
of functionality you seem to expect (although not your syntax) into
C++ over ten years ago, (ii) why that proposal was ultimately
rejected, and (iii) how you can accomplish this fairly easily in C++
by using a helper argument class. I've noticed that you posted
several of these "why does C++ do things in this way" kind of
questions, which leads me to believe you really need to pick up a copy
of that book - it will answer most or all the questions you pose, plus
if you're really interested in the answers to the questions you've
been posing (rather than just asking rhetorically)), you'll find it an
interesting read, since it describes the design philosophy underlying
the C++ language, and why the language incorporates some features but
not others. You can buy a used copy on Amazon for less than $10.
Check it out.

Best regards,

Tom
 
A

Agent Mulder

Agent Mulder said:
I want to know why this doesn't work

void f(int a=0,int b=1,int c=2){}
int main()
{
f(); //OK
f(1); //OK
f(1,2); //OK
f(1,2,3); //OK
f(,2,3)// NOT OK
return 0;
}

<Tom>
As others have said, what you've written doesn't work because it's not
C++. As to "why?," Stroustrup devotes several pages in his "The
Design and Evolution of C++" to (i) a proposal to incorporate the kind
of functionality you seem to expect (although not your syntax) into
C++ over ten years ago, (ii) why that proposal was ultimately
rejected, and (iii) how you can accomplish this fairly easily in C++
by using a helper argument class. I've noticed that you posted
several of these "why does C++ do things in this way" kind of
questions, which leads me to believe you really need to pick up a copy
of that book - it will answer most or all the questions you pose, plus
if you're really interested in the answers to the questions you've
been posing (rather than just asking rhetorically)), you'll find it an
interesting read, since it describes the design philosophy underlying
the C++ language, and why the language incorporates some features but
not others. You can buy a used copy on Amazon for less than $10.
Check it out.
</Tom>

The book advice is good. Thank you. For the kind of questions
I pose, I never felt comfortable defending the tool I use. I
try to keep the attitude of the 'amateur', amator in Latin.
Lover. But a scrupulous one, and not easily satisfied. If you find
it rhetoric, I think you mean suspicious.

-X
 
A

Agent Mulder

<Agent Mulder>
void f(int a=0,int b=1,int c=2){}
f(); //OK
f(1); //OK
f(1,2); //OK
f(1,2,3); //OK
f(,2,3);// NOT OK
</>

<Rainer>
f(1,,3); won´t work either, because that´s IMHO the way default params are
used in the C++ standard. you only can ommit the trailing params.
</>

I heard (read) that this issue is covered in Stroustrups 'Design and Evolution of C++' but I don't have it and so I cannot read it.
Questions like these might seem redundant, but hey, learning is all about redundancy.

I guess the reason C++ does not allow the above, is because of the name-mangling it performs on functions. When one parameter is
skipped and trailing params follow, the signature of the function is no longer intact. (Hope I use the right terms, it is quite some
time since I studied this). Now if that is the reason, one would still expect this to compile:

f(int,2,3);//NOT OK

-X
 
A

Alexander Terekhov

Agent Mulder wrote:
[...]
I heard (read) that this issue is covered in Stroustrups 'Design and
Evolution of C++' but I don't have it and so I cannot read it.

This issue is NOT covered in D&E. Done confuse this with "named
parameters"/"keyword arguments" (those are sort of covered, IIRC).
Questions like these might seem redundant, but hey, learning is all
about redundancy.

I guess the reason C++ does not allow the above, is because ...

Because no one complains (demands it) loud enough, I guess.

regards,
alexander.
 
A

Agent Mulder

<Alexander Terekhov>
no one complains (demands it) loud enough
</Alexander Terekhov>

struct Room
{
Room(bool a=true,bool b=true,bool c=true):Chair(a),Table(b),Bed(c){}
bool Chair,Table,Bed;
};
int main()
{
Room bedroom; //chair, table, bed
Room living(,,false); //no bed in the living
Room guestroom(,false,); //chair, bed, no table, no running water
Room toilet(,false,false); //no table, no bed
return 0;
}

The defective use of arguments above is a trivial case. I fill my
Windows structs in a function call and give a default value to each
formal argument. I want to apply a more elaborate use of default
arguments, but I am hindered by the fact that C++ does not allow
empty arguments.

Empty arguments would enable you to have one constructor or function
for whatever number and combination of arguments. You must only fill
in the non-default values and seperate them with the appropriate
number of comma's.

The semantics of this code is easily understood but the syntax is not
allowed in C++. The Annotated C++ Reference Manual:

<ARM 8.2.6>
It was felt that having empty arguments significant was not
only too subtle, but seriously decreased the opportunities
for detecting errors; an extra comma in an argument list is
not an unusual result of bad typing or sloppy editing.
</>

I don't agree on it being too subtle. It's only a matter of
counting comma's. I do feel a little offended however that the
language anticipates on my sloppy typing. There is a real need
for more flexible default arguments. The reasons not to have
them are not convincing enough.

(crippled version sent to comp.std.c++)

-X
 
K

Kevin Goodsell

Agent said:
<Alexander Terekhov>
no one complains (demands it) loud enough
</Alexander Terekhov>

struct Room
{
Room(bool a=true,bool b=true,bool c=true):Chair(a),Table(b),Bed(c){}
bool Chair,Table,Bed;
};
int main()
{
Room bedroom; //chair, table, bed
Room living(,,false); //no bed in the living
Room guestroom(,false,); //chair, bed, no table, no running water
Room toilet(,false,false); //no table, no bed
return 0;
}

I for one would not want to see this feature in C++. Unnecessary
features serve only to complicate an already complex language. The
effect you are seeking can be achieved (better) via other means. See the
"named parameter" idiom
(http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.17). Also, for
the case above you could very easily use the "ORing flags" idiom that
iostream classes use, and the result would have been much more clear.

-Kevin
 

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
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top