简体   繁体   中英

History of support for default first parameter in C++ function

As this question reiterates, a default parameter can be provided for the first argument in a C++ function. So the following

void foo(int a, int b=5); // good 
void foo(int a=5, int b=5); // used to be an error
void foo(int a=5); // used to be an error

However, I remember learning early on in my programming education that this was not allowed. At which point did this begin to change? What C++ standard removed this limitation? Since I did most of my early programming in Visual Studio, perhaps this wasn't even a standards issue, but a specific compiler limitation, if you so, do you also remember which (approximately) compiler versions had this limitation?

You say:

 void foo(int a=5); // used to be an error

This has never, ever been the case. You are remembering wrong.

I think you have misunderstood the question you referred to, and its answers. It is only required that if the nth parameter has a default value then all subsequent ones must also have default values. It has always been so. There has never been any specifiv rule that the first parameter cannot have a default value. Note that it is not important that the default parameters are all put in the same declaration. The following is also OK.

void f(int x = 9, int y);
//using f here is an error
void f(int x, int y = 10);
//using f here is OK now

The only official c++ standard of 1998 has them, so technically speaking the standard has always had them. Before this, even the often considered first quasi-standard for C++, the Bjarne Stroustrup book The C++ Programming Language mentions them. It was one of the first things Strousrup added to ANSI C when developing C++. But as the standard wasn't official, it's possible that there were compilers out there that did not support it.

As of today (June 2011), there is only one C++ standard. So it must be in the C++ standard of 1998.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM