Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is an extra comma not allowed in a parameter list when it is allowed in a brace initialization?

Following up on an old question of mine (Is there any relevance to an extra "," in the end of a brace initialization?)

Are there any technical reasons why the parameter list in function declarations and function calls has not been made code-generation-friendly like the brace initialization?

What I mean is:

This is ok, the extra , is ignored:

int generated_array[] = {
  1,
  2,
  3,
};

For consistency, wouldn't it also make sense to allow this?

int someFunc(
  int v1,
  int v2,
  int v3,
){...}

int ret_val = someFunc(
  1,
  2,
  3,
);

I cannot see how it would make compilation more complicated, but perhaps there is something I'm not thinking of. I would guess it would actually simplify it slightly.

Of course one can argue that it's not as useful as the brace initialization, but there should be cases where code generation would be made at least a tiny bit simpler if this was allowed.

like image 356
Martin G Avatar asked Mar 17 '15 12:03

Martin G


People also ask

Are trailing commas forbidden in JSON?

As JSON is based on a very restricted subset of JavaScript syntax, trailing commas are not allowed in JSON.

What is a dangling comma?

A trailing comma, also known as a dangling or terminal comma, is a comma symbol that is typed after the last item of a list of elements. Since the introduction of the JavaScript language, trailing commas have been legal in array literals. Later, object literals joined arrays.


1 Answers

We can find the rationale for allowing the trailing comma in an initializer-list in the Rationale for International Standard—Programming Languages—C which says:

K&R allows a trailing comma in an initializer at the end of an initializer-list. The Standard has retained this syntax, since it provides flexibility in adding or deleting members from an initializer list, and simplifies machine generation of such lists.

This rationale does not apply to the other cases.

This discussion on comp.lang.c++.moderated: Are comma-separated lists ending in a comma legal? also cites the same rationale.

like image 94
Shafik Yaghmour Avatar answered Sep 28 '22 14:09

Shafik Yaghmour