<分区>
#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 构造函数。