gpt4 book ai didi

c++ - 关于 std::move 行为

转载 作者:太空宇宙 更新时间:2023-11-03 10:41:52 25 4
gpt4 key购买 nike

我玩了一下 std::move,我注意到了一些奇怪的事情:

string && foo(string && x) {
string && a = move(x);
return move(a); //both x and a = "asdfgh"
}

int main(){

string x,aa;

aa = "asdfgh";
x = foo(move(aa));
//aa = "", x = "asdfgh"

aa = "asdf";
x = move(aa);
//aa = "", x ="asdf"
}

main 中的行为很清楚,但是为什么在调用 move 之后 foo 中的行为,x 却不是空的?为什么它没有从 x 中“窃取”?

最佳答案

因为,在 foo 中,a 是右值 referencea 和局部变量 x 是同一个对象。

关于c++ - 关于 std::move 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34251525/

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