gpt4 book ai didi

c++ - 这些右值引用是什么意思?

转载 作者:行者123 更新时间:2023-11-28 01:59:52 25 4
gpt4 key购买 nike

<分区>

#include <iostream>
#include <string>

using namespace std;

string&& f(string&& rvf)
{
cout << rvf << endl;

return (string&&)rvf;
// in the return statement, how rvf has become an lvalue?
// because of this I had to typecast explicitly
}

string&& g()
{
return "xyz";
}

int main()
{
string&& st = f("rvalue");

cout << "f " << st << endl;

string&& st2 = g();

cout << "g " << str2 << endl;
// why this line prints junk / crashes some times?

return 0;
}

一般问题:

当我们将右值赋给普通变量时,复制是如何完成的?右值究竟是如何“移动”到另一个变量中的?

复制后右值会发生什么变化?它会立即销毁还是在超出范围时销毁?

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