列出引用的初始化:GCC 或 Clang 正确吗?
List initialization of a reference: is GCC or Clang correct?
给出这个例子:
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.1 和 clang 7.0.1 被使用并且不同意这个例子中发生的事情。有人可以澄清一下吗?
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 ]
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
的情况下,该程序格式错误,因为它试图将非常量左值引用绑定到纯右值。在 iref3
的情况下,程序应该编译,因为 iref3
被绑定到纯右值结果 int{s}
.
给出这个例子:
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.1 和 clang 7.0.1 被使用并且不同意这个例子中发生的事情。有人可以澄清一下吗?
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 ]
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 typeE
and eitherT
is not a reference type or its referenced type is reference-related toE
... otherwise, ifT
is a reference type, a prvalue of the type referenced byT
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 “cv2T2
”, “cv1T1
” is reference-related to “cv2T2
” ifT1
is the same type asT2
, orT1
is a base class ofT2
.
因此,在您的代码中,引用类型 int
与初始化列表中的类型 S
没有引用相关。因此,通过 [dcl.init.list]/3,生成了一个 int
类型的纯右值,其形式为 int{s}
。正如注释所说,在 iref2
的情况下,该程序格式错误,因为它试图将非常量左值引用绑定到纯右值。在 iref3
的情况下,程序应该编译,因为 iref3
被绑定到纯右值结果 int{s}
.