Is it guaranteed that defaulted constructor initialize built in types automatically to 0?

╄→гoц情女王★ 提交于 2019-12-22 03:42:20

问题


Before you start to mark this as duplicate I've already read this .But It doesn't answer my question. The linked question talks about C++98 & C++03 but my question is about defaulted constructor introduced by C++11.

Consider following program (See live demo here):

#include <iostream>
struct Test
{
    int s;
    float m;
    Test(int a,float b) : s(a),m(b)
    { }
    Test()=default;
}t;
int main()
{
    std::cout<<t.s<<'\n';
    std::cout<<t.m<<'\n';
}

My question is that is the defaulted constructor provided by compiler here always initializes built in types to by default 0 in C++11 & C++14 when they are class & struct members. Is this behavior guaranteed by C++11 standard?


回答1:


Test = default will default initialize its members. but for type as int or float, default initialization is different than value-initialization

so

Test t; // t.s and t.m have unitialized value

whereas

Test t{}; // t.s == 0 and t.m == 0.0f;



回答2:


There are two separate questions built into this question.

  1. What does = default mean for the default constructor? From [class.ctor]:

    A default constructor that is defaulted and not defined as deleted is implicitly defined when it is odr-used (3.2) to create an object of its class type (1.8) or when it is explicitly defaulted after its first declaration. The implicitly-defined default constructor performs the set of initializations of the class that would be performed by a user-written default constructor for that class with no ctor-initializer (12.6.2) and an empty compound-statement.

    That is, Test() = default is exactly equivalent to Test() { }, which would default-initialize s and m, which sets them to some indeterminate value.

  2. How are t.s and t.m initialized? Yes, this is a separate question from (1) because we're not just calling the default constructor here. From [basic.stc.static]:

    All variables which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration.

    and from [basic.start.init]:

    Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place.

    Thus t.s and t.m are guaranteed to be 0, even though if we default-constructed a local Test, they would not be.




回答3:


As it is already answered, it is not guaranteed that default constructor will "initialize built in types automatically to 0".

You can use placement new to see this for yourself. Consider the following code:

#include <iostream>

struct Test
{
    int s;
    float m;
    Test(int a,float b) : s(a),m(b)
    { }
    Test()=default;
};

int main()
{
    char* buf = new char[sizeof(Test)];
    Test* t = new (buf) Test;

    std::cout << t->s << '\n';
    std::cout << t->m <<'\n';

    t->s = 42;
    t->m = 4.2;

    t->~Test();
    t = new (buf) Test;

    std::cout << t->s << '\n';
    std::cout << t->m <<'\n';
}

If default constructor was guaranteed to zero-initialize data members on non-class types, the output of this program would be four zeroes.

However, you'll likely see something like this instead:

0
0
42
4.2

Here's this code on cpp.sh - http://cpp.sh/9fdj




回答4:


No, the default constructor doesn't do anything! To initialize the member variables you can write:

struct Test
{
    int s = 0;
    float m = 3.3;
    Test(int a,float b) : s(a),m(b)
    { }
    Test()=default;
};

Look at this: C++ ctor = default




回答5:


For all purposes:

Test() = default;

is equivalent to:

Test() {}

So it does not initialize members of built-in types.



来源:https://stackoverflow.com/questions/33441087/is-it-guaranteed-that-defaulted-constructor-initialize-built-in-types-automatica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!