简体   繁体   English

左值引用绑定

[英]Lvalue reference binding

I have a question about wording in N3797::8.5.3/5 [dcl.init.ref] : 我对N3797::8.5.3/5 [dcl.init.ref]措词有疑问:

A reference to type “cv1 T1” is initialized by an expression of type “cv2 T2” as follows: 类型“ cv1 T1”的引用由类型“ cv2 T2”的表达式初始化,如下所示:

— If the reference is an lvalue reference and the initializer expression —如果引用是左值引用和初始值设定项表达式

  • is an lvalue (but is not a bit-field), and “cv1 T1” is reference-compatible with “cv2 T2,” or 是左值(但不是位字段),并且“ cv1 T1”与“ cv2 T2”参考兼容,或者

  • has a class type (ie, 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” 具有类类型(即T2是类类型),其中T1与T2无关,并且可以转换为类型为“ cv3 T3”的左值,其中“ cv1 T1”与“ cv3引用兼容” T3”

Does the second case mean the initializer expression is a class -type as well as an lvalue ? 第二种情况是否意味着初始值设定项表达式既是class类型又是lvalue

The clause says: 该条款说:

has a class type 具有类类型

so it must be a class type . 所以它必须是一个类类型 The examples further down in the document show it does not have to be an lvalue though, taking selected parts of the code example we have: 该文档后面的示例表明,尽管不一定要是左值,但采用代码示例的选定部分,我们可以:

struct A { };
struct B : A { operator int&(); } b;

int& ir = B(); // ir refers to the result of B::operator int&

B() is not a lvalue but as 8.5.3/5 says: B()不是左值,但正如8.5.3/5

can be converted to an lvalue of type “cv3 T3,” where “cv1 T1” is reference-compatible with “cv3 T3” 可以转换为类型“ cv3 T3”的左值,其中“ cv1 T1”与“ cv3 T3”具有参考兼容性

which in this case applies. 在这种情况下适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM