c – 复制列表初始化和传统复制初始化之间的任何区别?

c – 复制列表初始化和传统复制初始化之间的任何区别?,第1张

概述除了支持多个参数,禁止缩小转换,匹配构造函数采用std :: initializer_list参数,复制列表初始化与传统复制初始化有什么不同? 具体来说,假设有两个用户定义的类型A和B: class A {...};class B {...};B b;A a1 = {b};A a2 = b; 什么样的A和B的定义会对这两种初始化形式产生不同的影响?例如A和B有一定的定义,会使初始化合法, 除了支持多个参数,禁止缩小转换,匹配构造函数采用std :: initializer_List参数,复制列表初始化与传统复制初始化有什么不同?

具体来说,假设有两个用户定义的类型A和B:

class A {...};class B {...};B b;A a1 = {b};A a2 = b;

什么样的A和B的定义会对这两种初始化形式产生不同的影响?例如A和B有一定的定义,会使初始化合法,但另一个是非法的,或者是合法的,但是具有不同的语义,或者两者都是非法的,有不同的原因.

(假设A没有使用std :: initializer_List参数的构造函数)

编辑:添加一个与我有关的问题的链接:What is the supposed behavior of copy-list-initialization in the case of an initializer with a conversion operator?

解决方法 可能的是,新的拷贝列表初始化的行为被定义为“良好”且一致,但是由于向后兼容性,旧的复制初始化的“奇怪”行为无法改变.
正如你可以看到,这个子句中的列表初始化规则对于直接和复制形式是一样的.
与显式相关的差异仅在关于重载分辨率的章节中描述.但是对于传统的初始化直接和复制形式是不一样的.
传统和括号初始化是单独定义的,所以有一些(可能是无意的)微妙的差异总是有潜力的.

从标准的摘录中可以看出差异:

已经提到的差异

>不允许缩小转化次数
>多个参数是可能的
>支持语法优先于initializer-List构造函数,如果它们存在:

struct A{    A(int i_) : i (i_) {}    A(std::initializer_List<int> il) : i (*il.begin() + 1) {}    int i;}A a1 = 5; // a1.i == 5A a2 = {5}; // a2.i = 6

聚合的不同行为

对于聚合,您不能使用支持的复制构造函数,但可以使用传统的.

struct Aggr    {        int i;    };    Aggr aggr;    Aggr aggr1 = aggr; // OK    Aggr aggr2 = {aggr}; // ill-formed

在转换运算符存在时引用初始化的不同行为

支架初始化不能使用转换为参考类型的 *** 作符

struct S{    operator int&() { return some_global_int;}};int& iref1 = s; // OKint& iref2 = {s}; // ill-formed

4.通过其他类型的对象对类类型对象的初始化有一些微妙的差异

在本答复结束时,本标准的摘录中标有[*]的差异.

>旧的初始化使用用户定义的转换序列的概念(特别是需要复制构造函数的可用性,如上所述)
>大括号初始化在适用的构造函数中执行过载分解,即括号初始化不能使用转换为类类型的运算符

这些差异是对一些不太明显的(对我来说)的情况负责的

struct Intermediate {};struct S{    operator Intermediate() { return {}; }    operator int() { return 10; }};struct S1{    S1(Intermediate) {}};S s;Intermediate im1 = s; // OKIntermediate im2 = {s}; // ill-formedS1 s11 = s; // ill-formedS1 s12 = {s}; // OK// note: but brace initialization can use operator of conversion to intint i1 = s; // OKint i2 = {s}; // OK

5.重载分辨率的差异

>显式构造器的不同处理

见13.3.1.7通过列表初始化进行初始化

In copy-List-initialization,if an explicit constructor is chosen,the
initialization is ill-formed. [ Note: This differs from other
situations (13.3.1.3,13.3.1.4),where only converting constructors
are consIDered for copy initialization. This restriction only applIEs
if this initialization is part of the final result of overload
resolution. — end note ]

如果你可以看到更多的差异或某种方式纠正我的答案(包括语法错误),请做.

以下是current draft of the C++ standard的相关(但长时间)的摘录(我没有找到一种方法来将它们隐藏在扰流板下):
所有这些都位于第8.5章初始化程序中

8.5 Initializers

If the initializer is a (non-parenthesized) braced-init-List,the
object or reference is List-initialized (8.5.4).

If the destination type is a reference type,see 8.5.3.

If the destination type is an array of characters,an array of char16_t,an
array of char32_t,or an array of wchar_t,and the initializer is a
string literal,see 8.5.2.

If the initializer is (),the object is
value-initialized.

Otherwise,if the destination type is an array,
the program is ill-formed.

If the destination type is a (possibly
cv-qualifIEd) class type:

If the initialization is
direct-initialization,or if it is copy-initialization where the
cv-unqualifIEd version of the source type is the same class as,or a
derived class of,the class of the destination,constructors are
consIDered. The applicable constructors are enumerated (13.3.1.3),and
the best one is chosen through overload resolution (13.3). The
constructor so selected is called to initialize the object,with the
initializer Expression or Expression-List as its argument(s). If no
constructor applIEs,or the overload resolution is ambiguous,the
initialization is ill-formed.

[*] Otherwise (i.e.,for the
remaining copy-initialization cases),user-defined conversion
sequences that can convert from the source type to the destination
type or (when a conversion function is used) to a derived class
thereof are enumerated as described in 13.3.1.4,and the best one is
chosen through overload resolution (13.3). If the conversion cannot be
done or is ambiguous,the initialization is ill-formed. The function
selected is called with the initializer Expression as its argument; if
the function is a constructor,the call initializes a temporary of the
cv-unqualifIEd version of the destination type. The temporary is a
prvalue. The result of the call (which is the temporary for the
constructor case) is then used to direct-initialize,according to the
rules above,the object that is the destination of the
copy-initialization. In certain cases,an implementation is permitted
to eliminate the copying inherent in this direct-initialization by
constructing the intermediate result directly into the object being
initialized; see 12.2,12.8.

Otherwise,if the source type is a
(possibly cv-qualifIEd) class type,conversion functions are
consIDered. The applicable conversion functions are enumerated
(13.3.1.5),and the best one is chosen through overload resolution
(13.3). The user-defined conversion so selected is called to convert
the initializer Expression into the object being initialized. If the
conversion cannot be done or is ambiguous,the initialization is
ill-formed.

Otherwise,the initial value of the object being
initialized is the (possibly converted) value of the initializer
Expression. Standard conversions (Clause 4) will be used,if
necessary,to convert the initializer Expression to the cv-unqualifIEd
version of the destination type; no user-defined conversions are
consIDered. If the conversion cannot be done,the initialization is
ill-formed.

8.5.3 References …

8.5.4 List-initialization

List-initialization of an object or reference of type T is defined as
follows:

If T is an aggregate,aggregate initialization is
performed (8.5.1).

Otherwise,if the initializer List has no
elements and T is a class type with a default constructor,the object
is value-initialized.

Otherwise,if T is a specialization of
std::initializer_List<E>,a prvalue initializer_List object is
constructed as described below and used to initialize the object
according to the rules for initialization of an object from a class of
the same type (8.5).

[*] Otherwise,if T is a class type,
constructors are consIDered. The applicable constructors are
enumerated and the best one is chosen through overload resolution
(13.3,13.3.1.7). If a narrowing conversion (see below) is required to
convert any of the arguments,the program is ill-formed.

Otherwise,if the initializer List has a single element of type E and
either T is not a reference type or its referenced type is
reference-related to E,the object or reference is initialized from
that element; if a narrowing conversion (see below) is required to
convert the element to T,if
T is a reference type,a prvalue temporary of the type referenced by T
is copy-List-initialized or direct-List-initialized,depending on the
kind of initialization for the reference,and the reference is bound
to that temporary. [ Note: As usual,the binding will fail and the
program is ill-formed if the reference type is an lvalue reference to
a non-const type. —end note]

Otherwise,if the initializer List
has no elements,the object is value-initialized.

Otherwise,the program is ill-formed.

总结

以上是内存溢出为你收集整理的c – 复制列表初始化和传统复制初始化之间的任何区别?全部内容,希望文章能够帮你解决c – 复制列表初始化和传统复制初始化之间的任何区别?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1256013.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存