加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 03:28:18 所属栏目:百科 来源:网络整理
导读:除了支持多个参数,禁止缩小转换,匹配构造函数采用std :: initializer_list参数,复制列表初始化与传统复制初始化有什么不同? 具体来说,假设有两个用户定义的类型A和B: class A {...};class B {...};B b;A a1 = {b};A a2 = 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 == 5
A 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; // OK
int& 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; // OK
Intermediate im2 = {s}; // ill-formed
S1 s11 = s; // ill-formed
S1 s12 = {s}; // OK

// note: but brace initialization can use operator of conversion to int
int i1 = s; // OK
int 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读