15

Is the following code legal in C++?

void f(void* = 0)
{}

int main()
{
    f();
}

Which page of the C++ standard states that this usage is legal?

xmllmx
  • 39,765
  • 26
  • 162
  • 323
  • However it is, I cannot figure out in what contrived situation such a feature could actually have any use... – Matteo Italia Feb 03 '13 at 18:45
  • 3
    @MatteoItalia `enable_if` commonly. – Pubby Feb 03 '13 at 18:46
  • And how giving the parameter a name would change the situation? – Matteo Italia Feb 03 '13 at 18:46
  • 1
    @MatteoItalia gives you unused variable warnings mostly. – Pubby Feb 03 '13 at 18:47
  • @Pubby: `(void)parametername;` – Matteo Italia Feb 03 '13 at 18:48
  • 1
    @MatteoItalia: it wouldn't, but if you don't need a name, why giving one? – Andy Prowl Feb 03 '13 at 18:48
  • 3
    @MatteoItalia, One other "use" is if you have separate declarations and definitions and don't normally put the names in the declarations, but do put the default arguments in there. – chris Feb 03 '13 at 18:48
  • BTW we tend to use "chapter and verse" to reference standard passages, rather than logical or physical page numbers. – Lightness Races in Orbit Feb 03 '13 at 18:51
  • This question was useful for 'inline' definitions of a base class that does nothing, but has default argument. The derived class implementation uses the arguments. Of course, the base could be pure virtual. I think the compiler wouldn't give a diagnostic about the variable, in that case. But I think it is useful stylistically to indicate the parameter is unused. – artless noise Jul 07 '22 at 17:41
  • This is useful when you have a parameterized method in an abstract class `class A { void foo(int); }`, but your implementation of that method doesn't care about the parameter `class C: A { void foo(int = 0) override { ... }`, so you can call your method without arguments without introducing another method `C c; c.foo();`. This isn't _that_ contrived is it? – ardnew Jul 15 '23 at 03:38

4 Answers4

16

Yes, it's legal.

There is no standard wording to allow this combination of features specifically; there simply isn't any to disallow it, either.

Default argument syntax applies to function parameters in a parameter-declaration:

[C++11: 8.3.6/1]: If an initializer-clause is specified in a parameter-declaration this initializer-clause is used as a default argument. Default arguments will be used in calls where trailing arguments are missing.

...and function parameters in a parameter-declaration may be unnamed:

[C++11: 8.3.5/11]: [..] An identifier can optionally be provided as a parameter name. [..]

There is even an example of this usage under 8.3.6/4 (though examples are not normative text, so this cannot be used to prove anything concretely).

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
15

Yes, it's perfectly legal. An obvious example is found in N3485 8.3.6 Default Arguments/4:

[Example: the declaration

void point(int = 3, int = 4);  

declares a function that can be called with zero, one, or two arguments of type int.

David G
  • 94,763
  • 41
  • 167
  • 253
chris
  • 60,560
  • 13
  • 143
  • 205
  • 2
    Examples aren't normative ;) – Lightness Races in Orbit Feb 03 '13 at 18:48
  • @LightnessRacesinOrbit, I'm looking :) It's not like it explicitly says "Default arguments can be unnamed." I imagine it goes off of the basis that it shares properties with non-defaulted parameters. – chris Feb 03 '13 at 18:49
  • Okay, I got a confusing proof set up, but Light did a much better job of it, so it's not worth trying to confuse people who come later :) – chris Feb 03 '13 at 19:20
1

Yes, it is legal.
The syntax productions given for function parameters in clause 8.3.5/1 allow a parameter declaration without an identifier, but with an assignment expression (as initialiser).

Bart van Ingen Schenau
  • 15,488
  • 4
  • 32
  • 41
0

Not only is it legal, it could actually be quite useful depending on your coding style.

Default parameters are only meaningful in a function declaration.

Named parameters are only meaningful in a function definition.

f.h:

void f(void*=nullptr);

f.cc

void f(void* x)
{
...
}
Brice M. Dempsey
  • 1,985
  • 20
  • 16