Zero-Initialize array member in initialization list

23.1k views Asked by At

I have a class with an array member that I would like to initialize to all zeros.

class X
{
private:
    int m_array[10];
};

For a local variable, there is a straightforward way to zero-initialize (see here):

int myArray[10] = {};

Also, the class member m_array clearly needs to be initialized, as default-initializing ints will just leave random garbage, as explained here.

However, I can see two ways of doing this for a member array:

With parentheses:

public:
    X()
    : m_array()
    {}

With braces:

public:
    X()
    : m_array{}
    {}

Are both correct? Is there any difference between the two in C++11?

3

There are 3 answers

0
AudioBubble On BEST ANSWER

Initialising any member with () performs value initialisation.

Initialising any class type with a default constructor with {} performs value initialisation.

Initialising any other aggregate type (including arrays) with {} performs list initialisation, and is equivalent to initialising each of the aggregate's members with {}.

Initialising any reference type with {} constructs a temporary object, which is initialised from {}, and binds the reference to that temporary.

Initialising any other type with {} performs value initialisation.

Therefore, for pretty much all types, initialisation from {} will give the same result as value initialisation. You cannot have arrays of references, so those cannot be an exception. You might be able to construct arrays of aggregate class types without a default constructor, but compilers are not in agreement on the exact rules. But to get back to your question, all these corner cases do not really matter for you: for your specific array element type, they have the exact same effect.

0
AudioBubble On

The types of initialization can be kind of tedious to go through, but in this case it is trivial. For:

public:
    X()
    : m_array()
    {}

since the expression-list between the parentheses are empty, value-initialization occurs. Similarly for:

public:
    X()
    : m_array{}
    {}

list-initialization occurs, and subsequently value-initialization since the brace-init-list is empty.


To give a more comprehensive answer, let's go through §8.5 of N4140.

  1. If no initializer is specified for an object, the object is default-initialized. When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the object, that object retains an indeterminate value until that value is replaced (5.17).

This indeterminate value is what you refer to as garbage values.

  1. To zero-initialize an object or reference of type T means:

    — if T is an array type, each element is zero-initialized

  2. To value-initialize an object of type T means:

    — if T is a (possibly cv-qualified) class type ... then the object is default-initialized; ...

    — if T is an array type, then each element is value-initialized;

    — otherwise, the object is zero-initialized.

  3. The semantics of initializers are as follows. ... — If the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized (8.5.4).

    — If the initializer is (), the object is value-initialized.

So far it's clear that value initialization will make each element of the array zero since int is not a class type. But we have not yet covered list initialization and aggregate initialization, since an array is an aggregate.

§8.5.4:

  1. List-initialization of an object or reference of type T is defined as follows:

    — If T is an aggregate, aggregate initialization is performed (8.5.1).

And back to §8.5.1:

  1. If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from its brace-or-equal-initializer or, if there is no brace-or-equal-initializer, from an empty initializer list (8.5.4).

And we end with §8.5.4 again:

  1. List-initialization of an object or reference of type T is defined as follows:

    — Otherwise, if the initializer list has no elements, the object is value-initialized.

Since traversing the (draft) standard can take breath out of you, I recommend cppreference as it breaks it down pretty good.

Relevant links:

cppreference:

Draft standard:

1
JHumphrey On

Parentheses work in C++98, and are calling for zero initialization, which is what you want. I verified on gcc 4.3. Edit: removed incorrect statement about C++11. I also confirmed that empty braces perform empty-list-initialization using clang 3.4 with -std=c++11.