C ++(11):何时使用直接或复制初始化,如果两者都完美 [英] C++(11): When to use direct or copy initialization if both are perfectly fine

查看:142
本文介绍了C ++(11):何时使用直接或复制初始化,如果两者都完美的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在重复开始之前:我知道以下问题(以及其他一些)与这个问题很相关:

Before the shouts for duplicate begin: I am aware that the following question (and some others) are quite related to this one:

复制初始化和直接初始化之间的C ++有什么区别? / a>

Is there a difference in C++ between copy initialization and direct initialization?

这个问题的答案完全解释了复制初始化不可能的情况,并解释了两者之间的区别。但是,我的问题更微妙:

The answers in this question perfectly explains scenarios where copy initialization is not possible and explains the difference of the two and all that stuff. However, my question is more subtle:

请考虑代码:

A createA(){...}

A a1 = createA();
A a2(createA());

假设A可以隐式复制构造和所有东西,所以两个初始化 a1 a2 都没问题。在 A 的副本构造函数中没有副作用,因此这两个初始化在语义上也是等效的。 createA()直接返回变量的类型,而不是首先必须转换的其他值。我认为这种情况是很常见的。

Assume that A can be implicitly copy constructed and all that stuff, so both initializations of a1 and a2 are fine. There are no side effects in the copy constructor for A, so both initializations are also semantically equivalent. createA() returns directly the type of the variable, not something else that has to be cast first. I think this case is quite common.

所以,在这种情况下,两个选项同样适用和语义上等同,我应该使用哪一个?在规范中有建议还是在社区中达成共识/最佳实践,还是仅仅取决于我和我使用的编码风格?有没有C ++ 11介绍与旧标准相比的任何差异?

So, in this case, where both alternatives are equally applicable and semantically equivalent, which one should I use? Is there a recommendation in the spec or a consensus/best practice in the community or is it just up to me and my coding style which one to use? Has C++11 introduced any difference in comparison to older standards?

推荐答案

没有总是更好的答案,

There's no "always better" answer, it's a matter of style.

从对象类型的简单表达式(例如 createA())初始化对象时,我经常使用copy-init,可能只是因为熟悉 = 形式的赋值。否则,当初始化器是一个不同的类型或者有多个初始化器对象(例如多个构造函数参数或聚合init)我喜欢使用C + + 11列表初始化(也称为统一初始化语法),可以在更多地方,例如以初始化聚合以及具有用户定义构造函数的类,并且不能被解析为函数声明:

When initializing an object from a simple expression of the object's type (such as createA()) I often use copy-init, probably just due to familiarity with the = form of assignment. Otherwise, when the initializer is a different type or there are multiple initializers for the object (e.g. multiple constructor arguments, or aggregate init) I prefer to use C++11 list-initialization (a.k.a uniform initialization syntax), which can be used in more places, e.g. to initialize aggregates as well as classes with user-defined constructors, and cannot be parsed as a function declaration:

A a1{ createA() };

上述形式的list-init使用direct-init,而这种形式使用copy-init: / p>

The above form of list-init uses direct-init, whereas this form uses copy-init:

A a2 = { createA() };

当使用list-init时,我更喜欢direct-init形式,没有多余的 =

When using list-init I prefer the direct-init form without the redundant = sign.

有一些情况下,list-init是不可能的,例如当一个类型有一个初始化列表构造函数(即一个参数是一个 std :: initializer_list 的特殊化),并且你想调用一个不同的构造函数,但初始化-list构造函数将被选择,例如 std :: vector< int> v {5u,0}; 不会创建一个具有值为零的五个元素的向量,而是创建一个具有值为五和零的两个元素的向量

There are a few cases where list-init isn't possible, e.g. when a type has an initializer-list constructor (i.e. one taking a parameter that is a specialization of std::initializer_list) and you want to call a different constructor, but the initializer-list constructor would get chosen, e.g. std::vector<int> v{ 5u, 0 }; will not create a vector of five elements with value zero, but a vector with two elements with values five and zero

这篇关于C ++(11):何时使用直接或复制初始化,如果两者都完美的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆