Reputation: 778
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? If this is true and if I write int y = 5; foo(static_cast<int&&>(y));
, does this cause another temporary to be created or will the compiler intelligently refer to y?
Upvotes: 19
Views: 4337
Reputation: 41321
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:
A glvalue, class prvalue, or array prvalue of type “
cv1 T1
” can be cast to type “rvalue reference tocv2 T2
” if “cv2 T2
” is reference-compatible with “cv1 T1
”.
A temporary will be created if for example you call foo(1);
.
Upvotes: 14