也许我不是来自这个星球,但在我看来,以下应该是一个语法错误:

int a[] = {1,2,}; //extra comma in the end

但事实并非如此。当这段代码在Visual Studio上编译时,我很惊讶,但我已经学会了不相信MSVC编译器,就c++规则而言,所以我检查了标准,它也是标准允许的。如果你不相信的话,语法规则可以参考8.5.1。

Why is this allowed? This may be a stupid useless question but I want you to understand why I am asking. If it were a sub-case of a general grammar rule, I would understand - they decided not to make the general grammar any more difficult just to disallow a redundant comma at the end of an initializer list. But no, the additional comma is explicitly allowed. For example, it isn't allowed to have a redundant comma in the end of a function-call argument list (when the function takes ...), which is normal.

那么,有什么特别的原因,这个多余的逗号是明确允许的吗?


当前回答

如果你这样做,它会很有用:

int a[] = {
  1,
  2,
  3, //You can delete this line and it's still valid
};

其他回答

因此,当两个人在不同分支的列表中添加一个新项目时,Git可以正确地合并这些更改,因为Git是按行工作的。

它允许每一行都遵循相同的形式。首先,这样可以更容易地添加新行,并让版本控制系统有效地跟踪更改,还可以更容易地分析代码。我想不出技术上的原因。

我一直认为这样可以更容易地添加额外的元素:

int a[] = {
            5,
            6,
          };

只是变成了:

int a[] = { 
            5,
            6,
            7,
          };

在晚些时候。

由于向后兼容的原因,我认为允许使用尾随逗号。有很多现有的代码,主要是自动生成的,它们在后面放了一个逗号。它使得在结尾没有特殊条件的情况下更容易编写循环。 如。

for_each(my_inits.begin(), my_inits.end(),
[](const std::string& value) { std::cout << value << ",\n"; });

这对程序员来说并没有什么好处。

附注:虽然这样更容易自动生成代码,但实际上我总是注意不要在后面加上逗号,这样做的工作量最小,可读性得到了提高,这是更重要的。你写了一次代码,你读了很多次。

像许多东西一样,数组初始化器中的尾随逗号是c++从C继承的东西之一(并且必须永远支持)。在《Deep C secrets》一书中提到了与此完全不同的观点。

在一个有多个“逗号悖论”的例子之后:

char *available_resources[] = {
"color monitor"           ,
"big disk"                ,
"Cray"                      /* whoa! no comma! */
"on-line drawing routines",
"mouse"                   ,
"keyboard"                ,
"power cables"            , /* and what's this extra comma? */
};

我们读到:

...最后一个初始化式后面的逗号不是一个拼写错误,而是从原始c中继承下来的语法中的一个小点。它的存在或不存在是允许的,但没有任何意义。在ANSI C基本原理中声称的理由是,它使C的自动生成更容易。如果允许在每个以逗号分隔的列表中使用尾随逗号,例如在枚举声明中,或在单个声明中使用多个变量声明器,则声明将更加可信。事实并非如此。

... 对我来说,这更有意义