gpt4 book ai didi

c++ - 列出引用 : is GCC or Clang correct? 的初始化

转载 作者:可可西里 更新时间:2023-11-01 15:16:37 29 4
gpt4 key购买 nike

给出这个例子:

int g_i = 10;
struct S {
operator int&(){ return g_i; }
};

int main() {
S s;
int& iref1 = s; // implicit conversion

int& iref2 = {s}; // clang++ error, g++ compiles fine:
// `s` is converted
// to a temporary int and binds with
// lvalue reference

int&& iref3 = {s}; // clang++ compiles, g++ error:
// cannot bind rvalue reference
// to lvalue
}

错误如注释中所述。
gcc 8.2.1clang 7.0.1 被使用并且不同意这个例子中发生的事情。有人可以澄清一下吗?

In list initialization :

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 (by copy-initialization for copy-list-initialization, or by direct-initialization for direct-list-initialization); if a narrowing conversion (see below) is required to convert the element to T, the program is ill-formed.

Otherwise, if T is a reference type, a prvalue of the type referenced by T is generated. The prvalue initializes its result object by copy-list-initialization or direct-list-initialization, depending on the kind of initialization for the reference. The prvalue is then used to direct-initialize the reference. [ 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 ]

In reference initialization :

Given types “cv1 T1” and “cv2 T2”, “cv1 T1” is reference-related to “cv2 T2” if T1 is the same type as T2, or T1 is a base class of T2. “cv1 T1” is reference-compatible with “cv2 T2” if
- T1 is reference-related to T2, or
- T2 is “noexcept function” and T1 is “function”, where the function types are otherwise the same,

... and later on there's some (personally ambiguous) language on user-defined conversions :

例如:

If the reference is an lvalue reference and the initializer expression
...
has a class type (i.e., T2 is a class type), where T1 is not reference-related to T2, and can be converted to an lvalue of type “cv3 T3”, where “cv1 T1” is reference-compatible with “cv3 T3” (this conversion is selected by enumerating the applicable conversion functions ([over.match.ref]) and choosing the best one through overload resolution),
...
then the reference is bound to the ... value result of the conversion

...

Otherwise, if the initializer expression
...
has a class type (i.e., T2 is a class type), where T1 is not reference-related to T2, and can be converted to an rvalue or function lvalue of type “cv3 T3”, where “cv1 T1” is reference-compatible with “cv3 T3”
... then the value of the ... result of the conversion in the second case is called the converted initializer. If the converted initializer is a prvalue, its type T4 is adjusted to type “cv1 T4”

...

Otherwise:
- If T1 or T2 is a class type and T1 is not reference-related to T2, user-defined conversions are considered using the rules for copy-initialization of an object of type “cv1 T1” by user-defined conversion ... The result of the call to the conversion function, as described for the non-reference copy-initialization, is then used to direct-initialize the reference. For this direct-initialization, user-defined conversions are not considered.

...

Otherwise, the initializer expression is implicitly converted to a prvalue of type “cv1 T1”. The temporary materialization conversion is applied and the reference is bound to the result.

这些规则非常微妙,我无法完全掌握每种情况。对我来说,似乎应该生成一个纯右值(我同意 clang),但是关于引用初始化以及与列表初始化的交互的语言非常模糊。

最佳答案

让我们以正确的顺序阅读标准,以便我们知道哪些部分适用于手头的情况。

[dcl.init]/17 说:

The semantics of initializers are as follows... If the initializer is a (non-parenthesized) braced-init-list or is = braced-init-list, the object or reference is list-initialized (11.6.4) ...

所以我们转到 [dcl.init.list] (11.6.4)。第 3 段说:

List-initialization of an object or reference of type T is defined as follows: (... cases that don't apply are elided from this quotation...) 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 ... otherwise, if T is a reference type, a prvalue of the type referenced by T is generated. The prvalue initializes its result object by copy-list-initialization or direct-list-initialization, depending on the kind of initialization for the reference. The prvalue is then used to direct-initialize the reference. [ 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 ]

根据 [dcl.init.ref]/4:

Given types “cv1 T1” and “cv2 T2”, “cv1 T1” is reference-related to “cv2 T2” if T1 is the same type as T2, or T1 is a base class of T2.

因此,在您的代码中,引用类型 int 与初始化列表中的类型(即 S)没有引用相关。因此,通过 [dcl.init.list]/3,生成了 int 类型的纯右值,它采用 int{s} 的形式。正如注释所说,在 iref2 的情况下,该程序格式错误,因为它试图将非常量左值引用绑定(bind)到纯右值。在 iref3 的情况下,程序应该编译,因为 iref3 被绑定(bind)到纯右值结果 int{s}

关于c++ - 列出引用 : is GCC or Clang correct? 的初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54119925/

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