【发布时间】:2017-07-17 14:17:00
【问题描述】:
结构中的匿名结构中的大括号或等号初始化器不会对 VS2013 生成的输出执行其工作。代码如下:
#include <iostream>
#include <cstdint>
struct S
{
struct
{
uint64_t val = 0;
}anon;
};
int main()
{
S s;
S *a = new S;
std::cout << s.anon.val << std::endl;
std::cout << a->anon.val << std::endl;
return 0;
}
在 Linux 上使用此命令编译:
g++ -std=c++11 def-init-anon-atruct.cpp -o def-init-anon-atruct
(添加优化标志不影响结果)
预期结果:
0
0
很奇怪。用 VS2013 运行它会给出垃圾值。在实现 C++11 标准方面谁是正确的?我非常怀疑这是 GCC 的错。
是否与一些无用的 VS 编译器选项有关? Windows 扩展?由于 MS 的错误,我必须为结构创建默认构造函数?这太荒谬了。
【问题讨论】:
-
我也非常怀疑这是 gcc 的错。
-
英特尔的编译器在 Microsoft 目标平台上提供更好的语言一致性。
-
Visual Studio 2017 (Compiler v141) 给出了预期的结果:i.imgur.com/PcddbQ2.png
-
如果添加
anon = {};,VS2013 的输出是什么?
标签: c++11 member-initialization visual-c++-2013 anonymous-struct