gpt4 book ai didi

c++ - 右值引用的类型变化

转载 作者:太空狗 更新时间:2023-10-29 20:56:06 25 4
gpt4 key购买 nike

我可以知道为什么这段代码是有效的吗?谢谢。

  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.

关于c++ - 右值引用的类型变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34388808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com