类 C、构造函数和统一初始化之间有什么区别?

据我所知,在 C++ 中初始化变量有三种方法.

To the best of my knowledge, there are three ways to initialize a variable in C++.

int x = 0;    // C-like initialization
int x (0);    // Constructor initialization
int x {0};    // Uniform initialization

为 C++11 引入了统一初始化,以提供用于初始化不同类型变量的更统一的语法,在 C++03.

The uniform initialization was brought on for C++11 to provide a more uniform syntax for initializing different types of variables, which required different syntax in C++03.

类 C、构造函数和统一初始化之间有什么区别?我应该始终使用统一初始化吗?

What are the differences between C-like, constructor, and uniform initialization? And should I always use the uniform initialization?

推荐答案

首先,我建议看一下下面的谈话 由 Herb Sutter 撰写,他在其中就该主题提出了一些建议.大括号初始化讨论从大约 23:00 开始.

First, I would recommend looking at the following talk by Herb Sutter, in which he gives some recommendations about the subject. The brace-initialization discussion starts at around 23:00.

当您谈论原始数据类型时,所有 3 种都会产生相同的结果.我个人更喜欢坚持使用旧的 int x = 0 语法,但这取决于个人喜好.

When you are talking about primitive data types, all 3 yield the same result. I personally prefer sticking with the old int x = 0 syntax, but it comes down to personal preference.

对于类类型,大括号初始化和老式构造函数初始化不是完全可以互换的.例如:

For class types, brace initialization and old-school constructor initialization are not completely interchangeable. For example:

vector<int> v (100); // Creates a 100-element vector
vector<int> v {100}; // Creates a 1-element vector, holding the value 100.

这是因为 std::vector 有一个构造函数,将 std::initializer_list 明确定义为其唯一参数.请记住

This is because std::vector has a constructor that explicitly defines std::initializer_list as its only argument. Keep in mind that

auto var = {1, 2};

创建一个 std::initializer_list,以 var 作为其标识符.

creates a std::initializer_list, with var as its identifier.

关于初始化列表的事情是,它们提供了与之前可用的内容相比的可喜变化的一致性.例如,如果你要在 C++ 中初始化一个数组,你会使用:

The thing about initializer lists is that they provide consistency that is a welcome change from what was available beforehand. For example, if you were to initialize an array in C++, you would use:

int arr[] = {1, 2, 3, 4};

但是,如果你想用相同的元素初始化一个 vector<int>,你要么必须:

But, if you wanted to initialize a vector<int> with the same elements, you either had to:

  1. 先初始化上面的arr,然后通过arrarr + 4
  2. 单独或循环创建向量并 push_back() 元素.

使用 C++11,您可以使用

With C++11, you could just use

vector<int> v = {1, 2, 3, 4}; // Same syntax. Nice! Note that the = is optional

大括号初始化有用的另一个例子是它为 C++ 的最麻烦的解析提供了一种解决方法.从谈话中,假设我们有两个类,originextents,它们的实例可以被传递来构造另一个 rectangle 类型的对象.以下声明:

Another instance in which brace initialization is helpful is that it provides a workaround to C++'s most vexing parse. From the talk, assume that we have two classes, origin and extents, whose instances can be passed to construct another object of type rectangle. The following statement:

rectangle w(origin(), extents());

不允许您使用 originextents 临时对象创建 rectangle 对象,因为该语句被解析为函数声明.啧啧.所以通常,你必须这样做:

doesn't allow you to create a rectangle object using origin and extents temporaries, because that statement is parsed as a function declaration. Tsk tsk. So normally, you would have to do:

origin  o;
extents e;
rectangle w(o, e);

通过大括号初始化,您可以动态创建它们,并且

With brace initialization, you can create them on the fly, and

rectangle w {origin(), extents()};

将按预期工作,即传递给以 origin 对象作为其第一个参数和 extents 对象作为第二个参数重载的构造函数.

will work as intended, i.e. passed to the constructor which is overloaded with an origin object as it's first argument and an extents object as the second.

规则是针对对象的,除非你有理由不这样做,否则使用大括号初始化.

The rule is for objects, use brace initialiation unless you have a reason not to.

相关文章