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

牧云@^-^@ 提交于 2019-12-04 18:49:44

问题


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.


回答1:


The justification for trailing commas in initializer lists is to allow easy machine-generation of large static arrays. This way, if you happen to need to write a program which generates a C array initializer list, you can just write something like this:

printf("int arr[] = {");
for (int i = 0; i < N; i++) {
    printf("%d, ", i);
}
printf("};");

If the trailing comma wasn't permitted, you would have to make sure that it's not generated; and honestly, while it's not hard to do, it's just ugly and a pain in the neck.

There's no practical need to machine-generate large function parameter lists, though, and these lists admittedly look nicer without a trailing comma, so there's no need to permit the same thing in function parameters and calls.




回答2:


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.




回答3:


In my point of view this is related to the fact that early in C you could call functions before their declarations or functions were declared with an empty parameter list. In this case the compiler extract the information about parameters from a function call and its supplied arguments. In this case the trailing comma can be considered as an error that is it was unclear the intention of the programmer who wrote the call and it was not clear whether indeed the function has the number of parameters that corresponds to the number of arguments.

When you use an initializer list then all information is before your eyes. How many initializers you specified so many items were initialized or so many elements will have an array.




回答4:


I seem I recall that "a long time ago in a galaxy far away", C syntax permitted trailing comma for specifying that function has a variable number of parameters. Later it was changed to , ... syntax.

Correct me if I'm wrong.



来源:https://stackoverflow.com/questions/29099517/why-is-an-extra-comma-not-allowed-in-a-parameter-list-when-it-is-allowed-in-a-br

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!