C++11 list initialization

Monday morning, July 17, 2023:

I saw this usage when looking at the source code on GitHub today, so I studied it and recorded my research results as a blog


Table of contents

Why does C++11 introduce list initialization?

for example:

Uniform initialization syntax: initialization of objects and containers can be done in a uniform way

Prevent narrowing conversions


Why does C++11 introduce list initialization?

C++11 introduces the list initialization syntax for convenience and unification of the way objects and containers are initialized.

List initialization was introduced for several reasons:

  1. Uniform initialization syntax: Before C++11, there were a variety of object initialization methods, such as initialization with parentheses, initialization with equal signs, and initialization with curly braces. This leads to syntactic inconsistencies in initialization and increases code complexity. By introducing list initialization, a unified initialization syntax is provided, making initialization more concise and consistent.
  2.  Protection against narrowing conversions: List initialization has stricter type matching rules that prevent some narrowing conversions. For example, if an integer variable is initialized using list initialization, the compiler issues a warning or error if there is loss of precision or truncation, helping the programmer avoid potential problems.
  3.  Initializing Containers: The list initialization syntax makes initializing containers more convenient. Various containers such as arrays, vectors, maps, etc. can be initialized directly by using curly braces. This syntax is concise and easy to specify initial values.
  4.  Support for initialization of aggregate types: An aggregate type is a type that has no private or protected members, and does not define a constructor, destructor, or virtual function. In C++11, aggregate types can be initialized through list initialization, which makes it easier to initialize objects of structures or classes.

To sum up, the purpose of introducing list initialization syntax in C++11 is to provide a unified initialization method, increase the readability and simplicity of the code, and prevent some potential errors. It is part of the development of the C++ language and aims to improve development efficiency and code quality.
 

for example:

Uniform initialization syntax: initialization of objects and containers can be done in a uniform way

#include <iostream>
#include <string>
#include <vector>
#include <map>

class Person {
public:
    std::string name;
    int age;
};

int main() {
    // 使用列表初始化方式初始化Person对象
    Person person{"Alice", 20};
    // 使用列表初始化方式初始化vector容器
	std::vector<int> numbers{1, 2, 3, 4, 5};
    
    // 使用列表初始化方式初始化map容器
	std::map<std::string, int> scores{
	    {"Alice", 90},
	    {"Bob", 80},
	    {"Charlie", 95}
	};
    
    //输出person
    std::cout << "Name: " << person.name << std::endl;
    std::cout << "Age: " << person.age << std::endl;
    
 	//输出number
    std::cout << "Numbers: ";
    for (const auto& num : numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;
    
    std::cout << "Scores:" << std::endl;
	for (const auto& pair : scores) {
	    std::cout << pair.first << ": " << pair.second << std::endl;
	}
	    
    return 0;
}

Prevent narrowing conversions

#include <iostream>

int main() {
	//不使用列表初始化时,编译器不会发出警告
    int num1 = 5.5;
    
    // 使用列表初始化时,如果存在窄化转换,编译器会发出警告
    int num2{5.5}; // 窄化转换,编译器会发出警告
    
    std::cout << "num1: " << num1 << std::endl;
    std::cout << "num2: " << num2 << std::endl;
    
    return 0;
}

 

 

Guess you like

Origin blog.csdn.net/m0_61629312/article/details/131759863