简体   繁体   English

获取右值引用的地址是什么意思?

[英]What does it mean to take the address of an rvalue reference?

Given给定

void foo( int&& x ) {

    std::cout << &x;
}

This works but what does this address actually represent?这行得通,但这个地址实际上代表什么? Is a temporary int created when foo is called and that is what the address represents?调用 foo 时是否创建了一个临时int并且这就是地址所代表的内容? If this is true and if I write int y = 5; foo(static_cast<int&&>(y));如果这是真的并且如果我写int y = 5; foo(static_cast<int&&>(y)); int y = 5; foo(static_cast<int&&>(y)); , does this cause another temporary to be created or will the compiler intelligently refer to y? ,这会导致创建另一个临时变量还是编译器会智能地引用 y?

When you take an address of an rvalue reference, it returns a pointer to the object that reference is bound to, like with lvalue references. 当你获取一个右值引用的地址时,它返回一个指向引用绑定的对象的指针,就像左值引用一样。 The object can be a temporary or not (if for example you cast an lvalue to rvalue reference like you do in your code). 该对象可以是临时的或不是临时的(例如,如同在代码中一样,将左值转换为右值引用)。

int y = 5; foo(static_cast<int&&>(y)); does not create a temporary. 不会创造一个临时的。 This conversion is described in part 5.2.9/3 of the standard: 该转换在标准的第5.2.9/3部分中描述:

A glvalue, class prvalue, or array prvalue of type “ cv1 T1 ” can be cast to type “rvalue reference to cv2 T2 ” if “ cv2 T2 ” is reference-compatible with “ cv1 T1 ”. 如果“ cv2 T2 ”与“ cv1 T1 ”引用兼容,则可以转换类型为“ cv1 T1 ”的glvalue,类prvalue或数组prvalue以键入“对cv2 T2 rvalue引用”。

A temporary will be created if for example you call foo(1); 如果你调用foo(1);将创建一个临时的foo(1); .

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

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