我正面临值初始化与聚合初始化混合的问题。到目前为止,我试图依靠像这样进行所有初始化:
自动变量 = 类型{};
(是的,我知道大括号初始化器 ctor 与默认 ctor 的陷阱。所以请不要对此发表评论!)
我希望这将正确地“归零”或初始化 var 的内存。
但是在 VS 2013 Update 2 中,我看到了这个:
#include <string>
#include <iostream>
using namespace std;
struct B
{
double g[10];
std::string str;
};
struct C
{
double g[10];
};
struct A
{
double a[3];
double b = 0;
double d;
struct B b_stuff;
struct C c_stuff;
A() : b_stuff{}, c_stuff{} {}
};
int main()
{
auto a = A{};
double big[50] = {};
for(auto b : a.b_stuff.g) { cout << b << " "; }
cout << endl;
cout << endl;
for(auto b : a.c_stuff.g) { cout << b << " "; }
cout << endl;
cout << endl;
for (auto b : big) { cout << b << " "; }
return 0;
}
输出是这样的:
-9.25596e+061 -9.25596e+061 -9.25596e+061 -9.25596e+061 -9.25596e+061 -9.25596e+061 -9.25596e+061 -9.25596e+061 -9.25596e+061 -9.25596e+061
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
使用 GCC 4.7.2:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
我读了这个,但我看不出这种非归零行为的原因:
http://en.cppreference.com/w/cpp/language/value_initialization http://en.cppreference.com/w/cpp/language/aggregate_initialization
那么,VS 2013 有问题吗?为什么它不将 a.b_stuff.g 数组清零?