Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an unnamed parameter of function have a default value?

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?

like image 208
xmllmx Avatar asked Feb 03 '13 18:02

xmllmx


People also ask

Can parameters have default values?

Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.

What kind of parameters Cannot have a default value?

An IN OUT parameter cannot have a default value. An IN OUT actual parameter or argument must be a variable.

Can all the parameters of a function can be default parameters?

All the parameters of a function can be default parameters.

Which kind of parameters have a default value?

The default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined . In this case, the default value that we specify is applied by the compiler.


1 Answers

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).

like image 182
Lightness Races in Orbit Avatar answered Sep 28 '22 10:09

Lightness Races in Orbit