1

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?

Community
  • 1
  • 1
Alan Turing
  • 11,403
  • 14
  • 66
  • 114
  • 2
    There is nothing wrong with default value for the first parameter of a C++ function. The linked question is about restating the default values on the function declaration, which is not allowed. – Juliano Jun 15 '11 at 12:54
  • There is nothing wrong now, but I remember a time when it was wrong, and I'm trying to put that in historical perspective. I feel bad about this question (and it's potential lack of usefulness), but isn't it important to document the evolution of the language? – Alan Turing Jun 15 '11 at 13:05

4 Answers4

5

You say:

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

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

3

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
Armen Tsirunyan
  • 120,726
  • 52
  • 304
  • 418
3

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.

Tatu Lahtela
  • 4,396
  • 28
  • 29
  • Really? You think there were compilers that supported default arguments for only 2nd and subsequent parameters? – Armen Tsirunyan Jun 15 '11 at 16:56
  • I haven't seen one, but I wouldn't be surprised if there was one. In the early days there were all kinds of compilers out there with very mixed sets of features. – Tatu Lahtela Jun 16 '11 at 12:30
0

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

Didier Trosset
  • 33,178
  • 13
  • 75
  • 111