gpt4 book ai didi

c++ - N3797 的§8.5 p17(初始值设定项的语义)中是否缺少案例?

转载 作者:可可西里 更新时间:2023-11-01 16:39:57 24 4
gpt4 key购买 nike

对于聚合

struct S{int i, j;};

声明 S s({1, 2});S s({1}); 执行直接初始化到 N3797 §8.5 p16:

The initialization that occurs in the forms

T x(a);
T x{a};

as well as in new expressions (5.3.4), static_cast expressions (5.2.9), functional notation type conversions (5.2.3), and base and member initializers (12.6.2) is called direct-initialization.

但 §8.5 p17 似乎没有描述它们:

The semantics of initializers are as follows. The destination type is the type of the object or reference being initialized and the source type is the type of the initializer expression. If the initializer is not a single (possibly parenthesized) expression, the source type is not defined.

  • 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. [Note: An expression of type “cv1 T” can initialize an object of type “cv2 T” independently of the cv-qualifiers cv1 and cv2.

     int a;
    const int b = a;
    int c = b;

    end note ]

主题声明,S s({1, 2});S s({1});:

  • 不是列表初始化,因为每个初始化器都是带括号的braced-init-list
  • 目标类型不是引用
  • 目标类型通常不是字符数组。
  • 初始化器不是()
  • 目标类型不是数组。

最佳答案

此案例涵盖在标准中:它是 §8.5/17 第 6 项(强调我的):

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.

说明:我们首先注意到 S 是一个聚合(根据 §8.5.1/1)。但是聚合也是一个,因此有一个隐式声明的默认复制/移动构造函数(根据§12.8)。两个构造函数都有一个参数,因此是可行的(根据 §13.3.2)。他们的签名和往常一样:

S(const S&) //copy
S(S&&) //move

现在我们必须确定将初始化列表 {1,2} 转换为参数类型的转换顺序。 §13.3.3.1.5/6 指出:

Otherwise, if the parameter is a reference, see 13.3.3.1.4. [ Note: The rules in this section will apply for initializing the underlying temporary for the reference. —end note ]

由于参数类型是引用,§13.3.3.1.4/2 适用:

When a parameter of reference type is not bound directly to an argument expression, the conversion sequence is the one required to convert the argument expression to the underlying type of the reference according to 13.3.3.1. Conceptually, this conversion sequence corresponds to copy-initializing a temporary of the underlying type with the argument expression. Any difference in top-level cv-qualification is subsumed by the initialization itself and does not constitute a conversion.

由于 S 是一个聚合,我们必须应用 §13.3.3.1.5/5 来初始化这个临时对象:

Otherwise, if the parameter has an aggregate type which can be initialized from the initializer list according to the rules for aggregate initialization (8.5.1), the implicit conversion sequence is a user-defined conversion sequence with the second standard conversion sequence an identity conversion.

因此,我们最终到达了这个临时对象的聚合初始化。要确定这两个构造函数中哪一个是最佳可行函数,必须引用 §13.3.3.2(留给读者)。由于引用绑定(bind)到临时对象,因此实际上会选择移动构造函数。

关于c++ - N3797 的§8.5 p17(初始值设定项的语义)中是否缺少案例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20973417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com