Chapter 5 Technical Basis: 5.2 Zero Initialization

5.2 Zero Initialization

5.2 Zero initialization

 

For fundamental types such as int, double, or pointer types, there is no default constructor that initializes them with a useful default value. Instead, any noninitialized local variable has an undefined value:

For basic types (such as int, double, or pointer types), they do not have a default constructor to initialize a useful default value. In contrast, any uninitialized local variable has an undefined value:

void foo () 
{ 
    int x; // x is an undefined value 
    int * ptr; // ptr points to any position (instead of not pointing to any place) 
}

Now if you write templates and want to have variables of a template type initialized by a default value, you have the problem that a simple definition doesn’t do this for built-in types:

Now, if you want to write a template and want to initialize the variables of the template type with default values, then you will encounter the following problem: For built-in types, simple definitions cannot do this:

template <typename T>
 void foo () 
{ 
    T x; // x If T is a built-in type, then x is an undefined value 
}

For this reason, it is possible to call explicitly a default constructor for built-in types that initializes them with zero (or false for bool or nullptr for pointers). As a consequence, you can ensure proper initialization even for built-in types by writing the following:

For this reason, you can explicitly call the default constructor for built-in types, which initializes it to zero (false for bool and nullptr for pointers). Therefore, even for built-in types, you can ensure correct initialization by writing the following code:

template <typename T>
 void foo () 
{ 
    T x {}; // If T is a built-in type, x is 0 (or false) 
}

This way of initialization is called value initialization, which means to either call a provided constructor or zero initialize an object. This even works if the constructor is explicit.

This method of initialization is called "value initialization", which means that either the constructor is called or the object is initialized with zero. This can be done even when the constructor is declared as explicit:

 

Before C++11, the syntax to ensure proper initialization was

Before C ++ 11, the syntax to ensure proper initialization was

T x = T (); // If T is a built-in type, x is 0 (or false)

 

Prior to C++17, this mechanism (which is still supported) only worked if the constructor selected for the copy-initialization is not explicit. In C++17, mandatory copy elision avoids that limitation and either syntax can work, but the braced initialized notation can use an initializer-list constructor if no default constructor is available.

Prior to C ++ 17, this mechanism (still supported) was only selected when the copy initialization (using "=") was non-explicit. In C ++ 17, the mandatory "copy omitted" strategy avoids this limitation, and any kind of syntax can be used. However, if there is no default constructor, the initialization symbol of the braces can call the initialize_list constructor.

To ensure that a member of a class template, for which the type is parameterized, gets initialized, you can define a default constructor that uses a braced initializer to initialize the member:

In order to ensure that the class template member (the member has been parameterized) to obtain the initial value, you can use to define a constructor, the function uses a brace initializer to initialize the member.

Template <typename T>
 class MyClass {
 Private : 
    T x; 
public : 
    MyClass (): {x {} // make sure that for the built-in type of x may be initialized 
    } 
    ... 
};

The pre-C++11 syntax

Syntax before C ++ 11

MyClass (): x () { // Ensure that the built-in type x can also be initialized 
}

also still works.

Can work too.

 

Since C++11, you can also provide a default initialization for a nonstatic member, so that the following is also possible:

Starting from C ++ 11, you can also provide a default initial value for non-static members, so the following code is also possible:

template <typename T>
 class MyClass {
 private : 
    T x {}; // zero initialization x (unless otherwise specified) 

};

However, note that default arguments cannot use that syntax. For example,

However, please note that the default parameter value cannot use this syntax. E.g,

template<typename T>
void foo(T p{}) { //ERROR
    …
}

Instead, we have to write:

Instead, we must write

template <typename T>
 void foo (T p = T {}) { // OK (T () is required before C ++ 11) 

}

 

Guess you like

Origin www.cnblogs.com/5iedu/p/12731324.html
5.2
5.2