我的问题涉及如何返回没有复制构造函数的对象。作为示例,假设我有一些位于堆中的 bigResource
,假设我使用 unique_ptr
跟踪它。现在假设我将此资源的所有权授予毛毛虫。然后我有一个 CaterpillarWithBigResource
。现在在某个时候,这个 CaterpillarWithBigResource
将变成一个 ButterflyWithBigResource
,因此 Caterpillar
对象必须将所有权转移给 蝴蝶
对象。
我编写了以下代码来模拟这种情况:
#include <cstdlib>
#include <iostream>
#include <memory>
class ButterflyWithBigResource {
public:
// If I uncomment just this line, I get an error
// ButterflyWithBigResource(const ButterflyWithBigResource& other) = default;
// If I uncomment just this line, I get an error
// ButterflyWithBigResource(const ButterflyWithBigResource& other) = delete;
// With both above lines commented out, I get no errors, and the program runs fine.
ButterflyWithBigResource(std::unique_ptr<int>&& bigResource) :
bigResource(std::move(bigResource)) {
}
const int& getResource() {
return *bigResource;
}
private:
std::unique_ptr<int> bigResource;
};
class CaterpillarWithBigResource {
public:
CaterpillarWithBigResource(int bigResource) :
bigResource(new int(bigResource)) {
}
ButterflyWithBigResource toButterfly() && {
return ButterflyWithBigResource(std::move(bigResource));
}
private:
std::unique_ptr<int> bigResource;
};
/*
*
*/
int main(int argc, char** argv) {
CaterpillarWithBigResource caterpillarWithBigResource(5);
ButterflyWithBigResource butterflyWithBigResource(std::move(caterpillarWithBigResource).toButterfly());
std::cout << butterflyWithBigResource.getResource() << std::endl;
return 0;
}
请注意,Caterpillar
和Butterfly
都没有默认的复制构造函数,因为它们都有一个unique_ptr
。但是,我不认为这是个问题,所以只需要 move 构造函数。毕竟,我只是将所有权从 Caterpillar
转移到 Butterfly
。
事实上,当我用 g++ -c -g -std=c++11 -MMD -MP -MF
使用 g++
版本 4.8 编译程序时.2
它工作得很好。
但现在奇怪的是,如果我通过添加行 ButterflyWithBigResource(const ButterflyWithBigResource& other) = delete;
来提醒编译器 Butterfly
的复制构造函数被删除了,程序不再编译,编译器提示复制构造函数被删除,以至于我无法在toButterfly
方法中返回Butterfly
。
如果我尝试通过行 ButterflyWithBigResource(const ButterflyWithBigResource& other) = default;
来告诉它一切正常,我再次遇到同样的错误。
我想要发生的是将 toButterfly
方法中构造的 Butterfly
move 到 toButterfly
的返回地址,然后后来在 main()
中构造 butterflyWithBigResource
时用作 Butterfly
的 move 构造函数的参数。有没有办法做到这一点?
最佳答案
当您注释掉显式 default
和 delete
复制构造函数的两行时,编译器可以自由地隐式生成 move 构造函数(和 move 赋值运算符)你。
通过显式默认
或删除
复制构造函数,您可以抑制 move 构造函数的隐式生成。
来自 N3337,§12.8/9 [class.copy]
If the definition of a class
X
does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if
—X
does not have a user-declared copy constructor,
—...
当不再生成 move 构造函数时,必须复制 toButterfly()
的返回值,但无论您是否默认或删除了复制构造函数,该操作都会失败。
在您默认
复制构造函数的情况下,由于存在unique_ptr
数据成员(这是不可复制)。
当您删除
复制构造函数时,如果它是通过重载决议选择的,那将是一个错误。
您不必显式删除复制构造函数,因为如上所述,unique_ptr
数据成员的存在会隐式删除它,但如果您想这样做,那么您将还需要显式默认 move 构造函数(以及 move 赋值运算符,如果你想让 move 赋值工作的话)
ButterflyWithBigResource(ButterflyWithBigResource&&) = default;
ButterflyWithBigResource& operator=(ButterflyWithBigResource&&) = default;
关于c++ - 如何返回没有复制构造函数的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27814280/