在一个结构体内部的匿名结构体中使用大括号等于初始化器不能在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制造的一个错误,我必须为结构体创建默认构造函数?这太荒谬了。