0

Following this and this discussion about zero-initialization, I would like to clarify in which situation these two paragraphs zero-initialization CPP reference occur at the same time:

  1. As part of value-initialization sequence for non-class types and for members of value-initialized class types that have no constructors, including value initialization of elements of aggregates for which no initializers are provided.

From this paragraph I understand that a class member must have no constructor.

If T is an non-union class type, all base classes and non-static data members are zero-initialized, and all padding is initialized to zero bits. The constructors, if any, are ignored.

But from this one I understand that the constructor of class members, if any (they were supposed not to have), are ignored.

Then, in which case can I define a constructor for a class member and still ignore its constructor? I made an experiment with these two classes A and B, and constructing A{} calls B() (obviously) because c=1. Only B() = default; will make c=0.

class B
{
public:
    B() : c{1} {}
    int c;
};

class A
{
public:
    A() = default;
    B b;
};

Is it possible? Thank you in advance.

Victor
  • 460
  • 3
  • 11

1 Answers1

1

Then, in which case can I define a constructor for a class member and still ignore its constructor?

Static storage. A class instance with static storage duration will be zero initialised during the static initialisation phase which ignores constructors if any. The constructor is called later in the dynamic initialisation phase.

Could you give an example?

Here is an example of a class instance with static storage duration:

int main()
{
    static A a;
}
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • It is explicitly indicating "all base classes and **non-static data members** are zero-initialized". Is the CPP text wrong? – Victor Jan 26 '21 at 06:55
  • Could you give an example? I have been trying in different ways without any result – Victor Jan 27 '21 at 08:50
  • Sorry I tried it but still `c=1`, which means that `A` is calling `B`'s constructor. – Victor Jan 27 '21 at 10:36
  • @Victor Yes. That happens during the dynamic initialisation phase. – eerorika Jan 27 '21 at 10:59
  • Ok, then there is no practical effect, constructor always rules and rewrites the value. I was afraid that it was overruled. BTW I checked the code instructions and I didn't see any `mov 0` for `c`: https://godbolt.org/clientstate/eyJzZXNzaW9ucyI6W3siaWQiOjEsImxhbmd1YWdlIjoiYysrIiwic291cmNlIjoiY2xhc3MgQlxue1xucHVibGljOlxuICAgIEIoKSA6IGN7MX0ge31cbiAgICBpbnQgYztcbn07XG5cbmNsYXNzIEFcbntcbnB1YmxpYzpcbiAgICBBKCkgPSBkZWZhdWx0O1xuICAgIEIgYjtcbn07XG5cbmludCBtYWluKClcbntcbiAgICBzdGF0aWMgQSBhO1xufVxuIiwiY29tcGlsZXJzIjpbeyJpZCI6ImdzbmFwc2hvdCIsIm9wdGlvbnMiOiItc3RkPWMrKzE3In1dfV19 – Victor Jan 28 '21 at 02:36
  • @Victor I'm not an expert on this, but it is my understanding that the zero initialisation is handled in practice by the operating system before the execution of the application binary begins. – eerorika Jan 28 '21 at 06:48