gpt4 book ai didi

c++ - 返回类型和 move 语义

转载 作者:太空宇宙 更新时间:2023-11-04 16:15:41 24 4
gpt4 key购买 nike

<分区>

#include <iostream>
struct X
{
X(const char *) { std::cout << 1; }
X(const X&) {std::cout << 2;} //copy ctor;
X(X&& ) {std::cout << 3;} //Move ctor;
};

X f(X a)
{
return a; //a will be moved out of f calling X's move ctor
}

X g(const char* b)
{
X c(b);
return c;
}
int main()
{
f("hello");
g("hello");

//prints 131 and not 1313
}

为什么上面程序的输出是131?按照我的理解应该是1313

即当 f 返回时在同一行上调用 move 构造函数时,当 g 返回时也应调用 move 构造函数。

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