复合文字标准C ++? [英] Are compound literals Standard C++?
问题描述
复合字面是一个C99结构。即使我可以在C ++中这样做:
Compound Literals are a C99 construct. Even though I can do this in C++ :
#include <iostream>
using namespace std;
int main() {
for (auto i : (float[2]) {2.7, 3.1}) cout << i << endl;
}
似乎例如MSVC支持它附加信息。然而所有的编译器我可以得到我的手,编译上面提到的代码。
It seems that for example MSVC supports it as an extension. Yet all compilers I can get my hands on, compile the above mentioned code.
因此是C ++ 14中的一项功能吗?是否有不同的标准术语(在我看来,只是创建一个临时使用支撑的初始化)?
So is this a feature available in C++14 ? Is there a different standard term (It looks to me like just creating a temporary using braced initialization) ?
侧注:复合文字(或任何我应该称为上述内容)是包扩展上下文(
Side Note : "Compound Literals" (or whatever I should call the above) are a pack expansion context ( just to mention a functionality )
推荐答案
这是一个扩展, gcc 和 clang
支持。 gcc文档说:
This is an extension that both gcc and clang
support. The gcc document says:
作为扩展,GCC支持C90模式和C ++中的复合文字,虽然语义在C ++ 。
As an extension, GCC supports compound literals in C90 mode and in C++, though the semantics are somewhat different in C++.
如果您使用 -pedantic 您应该会收到警告,例如 clang
说( 查看实时 ):
if you build with -pedantic you should receive a warning, for example clang
says (see it live):
警告:复合文字是C99特定功能[-Wc99-extensions]
warning: compound literals are a C99-specific feature [-Wc99-extensions]
请注意,C ++中的语义差异并不重要,在C99中定义的代码在C ++中有这个扩展的未定义的行为:
Note, the semantic differences in C++ are not minor and code that would be well-defined in C99 can have undefined behavior in C++ with this extension:
在C ++中,复合字面值指定一个临时对象,其完全表达式的结束。因此,取得复合字面值的子对象的地址的定义良好的
C代码可以在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++.
这篇关于复合文字标准C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!