简体   繁体   English

值初始化与聚合初始化

[英]Value initialization vs aggregate initialization

I am facing an issue with value initialization mixed with aggregate initialization. 我在将值初始化与聚合初始化混合在一起时遇到问题。 So far, I tried to rely on doing all my initializations like this: 到目前为止,我试图依靠所有的初始化来进行如下操作:

auto var = Type{}; auto var = Type {};

(Yes, I am aware of brace-initializer ctor vs default ctor pitfall. So no comments about that please!) (是的,我知道大括号初始化器ctor与默认ctor陷阱。所以请对此无评论!)

I expect that this would properly "zero out" or init the memory of var. 我希望这将适当地将其“清零”或初始化var的内存。

But in VS 2013 Update 2, I see this: 但是在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;
}

The output is this: 输出是这样的:

-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

With GCC 4.7.2: 使用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 

I read this but I don't see reason for this non-zeroing behaviour: 我读了这篇文章,但没有看到这种非归零行为的原因:

http://en.cppreference.com/w/cpp/language/value_initialization http://en.cppreference.com/w/cpp/language/aggregate_initialization http://en.cppreference.com/w/cpp/language/value_initialization http://en.cppreference.com/w/cpp/language/aggregate_initialization

So, is VS 2013 buggy? 那么,VS 2013越野车是不是? Why doesn't it zero out the a.b_stuff.g array? 为什么不将a.b_stuff.g数组归零?

Visual C++ has a long and storied history of value initialization bugs . Visual C ++具有悠久的,有历史价值初始化错误的历史 I believe Bug 746973 is the one you have stumbled across here. 我相信Bug 746973是您偶然发现的bug

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM