右值引用的类型变化
Type change in rvalue reference
请问为什么这段代码是valid.Thanks.
double i=5;//int i=5 will invalidate this piece of code
int && r1=i;
std::cout<<r1<<std::endl;
[dcl.init.ref]/1 A variable declared to be a T&
or T&&
, that is, "reference to type T
" (8.3.2), shall be initialized by an object, or function, of type T
or by an object that can be converted into a T
.
[dcl.init.ref]/5 A reference to type "cv1 T1
" is initialized by an expression of type "cv2 T2
" as follows:
...
(5.2.2.2) -- If T1
is a non-class type, a temporary of type "cv1 T1
" is created and copy-initialized (8.5) from the initializer expression. The reference is then bound to the temporary.
请问为什么这段代码是valid.Thanks.
double i=5;//int i=5 will invalidate this piece of code
int && r1=i;
std::cout<<r1<<std::endl;
[dcl.init.ref]/1 A variable declared to be a
T&
orT&&
, that is, "reference to typeT
" (8.3.2), shall be initialized by an object, or function, of typeT
or by an object that can be converted into aT
.[dcl.init.ref]/5 A reference to type "
cv1 T1
" is initialized by an expression of type "cv2 T2
" as follows:...
(5.2.2.2) -- If
T1
is a non-class type, a temporary of type "cv1 T1
" is created and copy-initialized (8.5) from the initializer expression. The reference is then bound to the temporary.