复合文字标准C?

Compound Literals是一个C99结构。即使我可以在C:

#include <iostream>
using namespace std;

int main() {
    for (auto i : (float[2]) {2.7, 3.1}) cout << i << endl;
}

似乎例如MSVC支持它as an extension.然而,所有的编译器,我可以得到我的手,编译上述代码。

这是C 14中的一个功能吗?有不同的标准术语(看起来像我刚刚创建一个临时使用支撑初始化)?

侧注:“复合文字”(或任何我应该称之为上述)是pack expansion context(只是提及功能)

这是gcc和clㄧ支持的延伸。 gcc文件说:

As an extension, GCC supports compound literals in C90 mode and in C++, though the semantics are somewhat different in C++.

如果你搭建-pedantic,你应该收到一个警告,比如说clang说(see it live):

warning: compound literals are a C99-specific feature [-Wc99-extensions]

注意,C中的语义差异不是很小,在C99中定义明确的代码可能在C中具有未定义的行为,具有此扩展名:

In C++, a compound literal designates a temporary object, which only
lives until the end of its full-expression. As a result, well-defined
C code that takes the address of a subobject of a compound literal can
be undefined in C++.

翻译自:https://stackoverflow.com/questions/28116467/are-compound-literals-standard-c

转载注明原文:复合文字标准C?