gpt4 book ai didi

C++11右值引用题

转载 作者:太空狗 更新时间:2023-10-29 21:02:46 25 4
gpt4 key购买 nike

我正在尝试了解 C++11 的新特性,但我有两个关于右值引用的问题。

  1. 我还没有看到函数返回右值引用的示例(std::move 除外)。是否存在这样做有意义的情况?例如,int&& foo(...)

  2. 在声明局部变量或作为函数参数时,是否存在对 const 进行右值引用有意义的情况?例如,foo(int const&& i)

最佳答案

1) I have not seen examples when functions return rvalue references (except std::move). Are there cases when it makes sense to do so?

除了库函数之外,它们所做的只是将它们的参数转换为右值引用(例如 std::move 和适当时的 std::forward) 并返回那个论点,我不相信你会经常遇到这种情况。

2) Are there cases when it makes sense to have rvalue references to const?

不多。实际上几乎没有。当你想绑定(bind)到一个你最终想从中移动的对象时,使用右值引用,因为你不能从 const 对象中移动(因为移动会改变对象,而 const promise 相反),对 const 的右值引用毫无意义。另请注意,lvalueconst 的引用也可以绑定(bind)到右值。有a few use cases for that虽然。

关于C++11右值引用题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14922480/

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