Madagascar
Madagascar

Reputation: 7345

Rationale for allowing unnamed parameters in function definitions

From Annex M of this document:

— parameter names may be omitted in function definitions;

One use I see for this is not having to cast anything to void, or using an unused attribute. But was that the rationale? If not, where's this useful? Unnamed parameters can not be accessed, can they?

Upvotes: 0

Views: 177

Answers (2)

Luis Colorado
Luis Colorado

Reputation: 12708

The rationale is easy. A function without a parameter name, expresses a function with a parameter in it's interface, but probably a parameter that is not used inside this function.

On reusable code is frequent to have several functions that implement the same interface, as many of them are passed as parameters as function pointers to other functions, so they must convey to some fixed interface. On the other side, some of those functions will not use some of the parameters at all, but the list of parameters must be conserved for the compiler to know how to prepare the calling code to the function.

The compiler can make a check if you have mispelled the parameter name inside the function because it will detect that the parameter is not used inside the body, and this can help you detect that the parameter is not referenced inside the function.

On other side, if you don't need to use the parameter, you can tell the compiler I know there's a parameter in the parameter list that is not used, but this is done on purpose, and is not the result of a coding mistake.

On another third situation, when you need to indicate a generic function interface, you generally don't need to specify the parameter names, only the types required at each position, so is not worth putting them at all (but for documentation purposes is good to have the parameter name indicate what it is used for)

Upvotes: 1

Lundin
Lundin

Reputation: 214770

The rationale was that some parameters from the function declaration (where identifiers were always optional) may not actually be used in the function definition and could therefore be allowed to be omitted.

This feature was described and proposed in the document n2510 where you can read the rationale straight from the source.

As for the "not having to cast to void", that's now also possible with [[maybe_unused]], which can also be used for the function result. So personally I find the unnamed parameters feature quite redundant.

Upvotes: 2

Related Questions