1. Using list initialization
In c++98/03, there are many ways to initialize an object, such as
int ar[3] = {1,2,3};int arr[] = {--); Normal array struct a{ int x; struct b{ int y; int z; } b;} A = {1, {3,4}}; The pod type, which can be copied directly using the memcpy object int i = 0; Foo foo = f;//copy initialize int i (0); Foo f (123); Direct initialization
In c++98/03, the common number combination pod type can be initialized by initializing the list. C++11, an initialization list can be used to initialize any type of object.
List initialization is to give a ={} after a variable, or just a {}, the parentheses contain the required data.
Class Foo{public: foo (int) {};p rivate: foo (const foo&);}; int main () { Foo A1 (123); Foo A2 = 123; The implicit conversion converts 123 into a Foo object and then calls operator= for copy construction. //error, cannot use copy constructor Foo a3 = {123}; List initialization, although using =, but still list initialization, private copy construction invalid Foo a4{123}; List initialization int a5 = {1}; int a6{2}; The new operator can be initialized with parentheses, or it can be initialized with a list of int* a = new Int{1, 2, 3}; Double b = double{12.00}; int* arr = new int[3]{2,4,5}; List initialization can also be directly used on the function's return value on the struct foo{ Foo (int, double) {}; }; Foo func (void) { return {123, 12.00};//returns the constructed Foo object, using list initialization } return 0;}
2. Details of the use of list initialization
C++11 when the list is initialized, the object that needs to be initialized is the aggregate.
C + + aggregates
Definition of aggregation type:
(1) Type is an ordinary array
(2) The type is a class, and:
(a) No user-defined constructors
(b) disinterested or protected non-static data members
(c) No base class
(d) No virtual function
(e) Non-static data members that cannot have {} and = directly initialized
For the case (e), for example a struct st{ int x; Double y = 0.0;}; ST S{1, 2.3}; Error, cannot have {} or = directly initialized non-static data member
In C++98/03, Y, a non-static data member, cannot itself be initialized at the time of declaration, but in c++11, non-static data members can also initialize at the same time (that is, using {} or = initialization).
struct st{ int x; int y; ST (int A, int b): x (0), y (0) {};}; ST st{1, 2}; When this is done initially, the list is not initialized, but the constructor is called. Therefore, St.x = 0, St.y = 0.
The definition of an aggregation type is not recursive, that is, when a non-static member of a class is not an aggregate type, the class may also be an aggregation type.
struct st{ int x; Double y;private: int z;}; St S{1, 2.4, 1};//error, not aggregation type, unable to use list initialization of struct foo{ St; int x; Double y;}; Foo foo{{}, 1, 2.3};//Although ST is not an aggregation type, Foo is an aggregation type. For non-aggregate type member ST initialization, * * Use empty {}, which is equivalent to calling the parameterless constructor * *
C++11 Initialization list Assignment
For an aggregation type, the use of an initialization list is equivalent to assigning values to each of these elements, whereas for a non-collection type, you need to customize an appropriate constructor first by using the initialization list to invoke its corresponding constructor.
3. Initialize the list
The STL container in C++11 has the same initialization capability as an array of the specified length, with the following code:
int arr[]{1,2,3};std::map<std::string,int>mm={{"123", 1}, {"Hello", 2}, {"Fuck", 3}};std::set<int> = { (a);
The container in STL is implemented by using std::initializer_list, a lightweight template.
std::initializer_list
(1) It is a lightweight container type that internally defines the concepts necessary for containers such as iterator
(2) for std::initializer_list< t>, it can accept an arbitrary-length initialization list, but requires that the element be of type T
(3) It has three member interfaces: size (), Begin (), End ()
(4) It can only be initialized or assigned as a whole, and cannot modify one of its elements individually (because the previous bar knows it does not provide an interface)
(5) It is stored inside the reference of the element, so the copy operation is not done , so it is necessary to pay attention to the use of the internal elements to ensure that the validity period
Std::initializer_list<int> list = {1,2,3,4}; size_t n = list.size (); 4 list = {4, 5, 6};//Overall assignment
4. Prevent type narrowing
Type narrowing refers to an implicit type conversion that causes data content to change or loss of precision. Type narrowing consists of the following:
(1) Implicit conversion from a floating point to an integer number
(2) converting from high-precision floating-point numbers to low-precision floating-point numbers
(3) implicitly converted from an integer to a floating-point number, and beyond the floating-point representation range, such as float x= (unsigned long)-1;
(4) Converting from one integer to a shorter integer, and exceeding the representation range of a short length integer. such as char x = 3333;
In c++98/03, the compiler does not get an error when the above type narrowing occurs. in c++11, you can check and prevent type narrowing by initializing the list.
int a = 1.1; can be int a = {1.1};//Error
c++11--List Initialization