问题描述
这是我的一个老问题,跟进(Is有任何相关性的额外","在括号初始化结束)
Following up on an old question of mine (Is there any relevance to an extra "," in the end of a brace initialization?)
有没有为什么参数列表中的函数声明和函数调用尚未作出code世代友好像括号初始化?任何技术原因
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?
我的意思是:
这是确定的,额外的,
被忽略了:
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.
当然,我们可以说,它不是作为支柱初始化是有用的,但应该在哪里code一代人进行至少一点点简单的,如果这是允许的情况下。
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.
推荐答案
在初始化列表尾随逗号的理由是为了便于机器新一代大型静止阵列。这样,如果你碰巧需要编写生成C数组初始化列表中的程序,你可以这样写:
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.
这篇关于为什么一个额外的逗号不是参数列表,当它被允许在括号初始化允许?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!