下编译没有错误:返回布展仅式编译即使拷贝构造函数是不可用
#include <memory>
std::unique_ptr<int> f() {
std::unique_ptr<int> x(new int(42));
return x;
}
int main() {
std::unique_ptr<int> y = f();
}
我认为的f()
返回值被x
复制初始化,但std::unique_ptr
是MOVE-只有类型。这是不是因为复制构造函数不可用而不合格?标准中的相关条款是什么?有没有什么地方说,如果f()
是一种只能移动的类型而不是返回语句变成移动结构而不是复制结构?
不知道,如果它是一个重复的[C++返回值优化(HTTP: //stackoverflow.com/questions/19454068/c-return-value-optimization)。 – iammilind 2015-02-09 13:15:03
@iammilind:不是。 – 2015-02-09 13:19:06
http://stackoverflow.com/questions/4316727/returning-unique-ptr-from-functions – vsoftco 2015-02-09 13:22:09