Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 507
Destructor Avatar asked Oct 30 '15 16:10

Destructor


People also ask

Does default constructor initialize 0?

As default constructor initializes the data members of class to 0.

Is default constructor created automatically?

The default constructor is auto-generated if there is no user-declared constructor (§12.1/5).

What happens when default constructor is called?

In both Java and C#, a "default constructor" refers to a nullary constructor that is automatically generated by the compiler if no constructors have been defined for the class. The default constructor implicitly calls the superclass's nullary constructor, then executes an empty body.

What is an automatic default constructor and what does it do?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .

Does the default constructor of a class initialize the built-in types?

Implicitly defined (by the compiler) default constructor of a class does not initialize members of built-in types. However, you have to keep in mind that in some cases the initialization of a instance of the class can be performed by other means.

What is the use of default constructor in Java?

This method is not only used in classes but also used with struct and union data types A Default Constructor is a constructor type in Classes that is called when class is defined with no arguments, or it is defined with an empty parameter list, or with default arguments provided for every parameter.

What is the default constructor for int in C++?

C++ allows even built-in type (primitive types) to have default constructors. The function style cast int() is analogous to casting 0 to required type. The program prints 0 on console. The initial content of the article triggered many discussions, given below is consolidation.

Do built-in types have constructors?

Built-in types are considered to have constructors”. The code snippet above mentioned int () is considered to be conceptually having constructor. However, there will not be any code generated to make an explicit constructor call. But when we observe assembly output, code will be generated to initialize the identifier using value semantics.


2 Answers

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.

like image 133
Barry Avatar answered Sep 20 '22 23:09

Barry


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;
like image 35
Jarod42 Avatar answered Sep 17 '22 23:09

Jarod42