473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

default arguments

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
Jul 19 '05 #1
8 6636
> 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


Jul 19 '05 #2
On Thu, 4 Sep 2003 12:31:08 +0200, Agent Mulder wrote:
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.
Jul 19 '05 #3
Tom
"Agent Mulder" <mb*******************@home.nl> wrote:
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
Jul 19 '05 #4

"Tom" <Th***********@yahoo.com> wrote in message
news:7b**************************@posting.google.c om...
"Agent Mulder" <mb*******************@home.nl> wrote:
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

Jul 19 '05 #5
<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
Jul 19 '05 #6

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.
Jul 19 '05 #7
<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


Jul 19 '05 #8
Agent Mulder wrote:
<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-lit...tml#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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
14
by: Edward Diener | last post by:
In the tutorial on functions there are sections on default arguments and keyword arguments, yet I don't see the syntactic difference between them. For default arguments the tutorial shows: def...
12
by: earl | last post by:
class temp { public: temp(); foo(char, char, char*); private: char matrix; }; temp::foo(char p, char o, char m = matrix )
18
by: Dan Cernat | last post by:
Hi there, A few threads I had a little chat about default values. I am starting this thread because I want to hear more opinions about the default values of function parameters. Some say they...
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
4
by: aling | last post by:
What's the rule of default argument of function in C++? I found that the default argument of function could not necessary be a constant value. Is it true? Previously I thought that the default...
10
by: Alan G Isaac | last post by:
My class MyClass reuses many default parameters with a small number of changes in each instance. For various reasons I decided to put all the parameters in a separate Params class, instances of...
12
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
35
by: bukzor | last post by:
I've found some bizzare behavior when using mutable values (lists, dicts, etc) as the default argument of a function. I want to get the community's feedback on this. It's easiest to explain with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.