vector<unique_ptr> 的初始化因复制错误而失败
Initialization of vector<unique_ptr> fails with copy error
我正在努力正确初始化 std::unique_ptr
中的 std::vector
。
示例代码:
#include <iostream>
#include <vector>
#include <memory>
class Base{
public:
std::string getString() { return this->string; };
protected:
std::string string;
};
class Derived: public Base{
public:
Derived(std::string bla){
this->string = bla;
}
};
class Collection{
protected:
std::vector<std::unique_ptr<Base>> mappings;
};
class DerivedCollection: public Collection{
public:
DerivedCollection(std::string bla){
std::vector<std::unique_ptr<Base>> maps;
maps.push_back(std::make_unique<Derived>(bla));
//or this: (does not work aswell)
//maps.emplace_back(new Derived(bla));
this->mappings = maps;
}
};
int main(int argc, char** argv){
DerivedCollection test = DerivedCollection("bla");
return 0;
}
不知何故,仅定义 mappings
会触发错误:
/usr/include/c++/6.3.1/bits/stl_construct.h:75:7:
error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Base; _Dp = std::default_delete<Base>]’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
这告诉我,我设法从 const unique_ptr 构造了一个 unique_ptr,这不起作用,因为 unique_ptr 不可复制构造。
不知何故,即使我在 DerivedCollection
构造函数中注释所有内容,这仍然会失败。
我的猜测是我需要一个适合 Collection
class 的构造函数。不过我不确定如何定义它。
有什么想法吗?
-- 麦芽糖
maps
是不可复制的,因为它是 unique_ptr
的 vector
。将其移至 mappings
可解决问题:
this->mappings = std::move(maps);
您的代码还有其他问题:
你应该使用成员初始化列表来初始化数据成员而不是构造函数体.
getString
可以 return const std::string&
避免复制。
Derived
的构造函数可以 std::move
bla
进入数据成员。
test
可以初始化为:DerivedCollection test{"bla"}
.
永远不要使用 new
- 请改用 make_unique
。
我正在努力正确初始化 std::unique_ptr
中的 std::vector
。
示例代码:
#include <iostream>
#include <vector>
#include <memory>
class Base{
public:
std::string getString() { return this->string; };
protected:
std::string string;
};
class Derived: public Base{
public:
Derived(std::string bla){
this->string = bla;
}
};
class Collection{
protected:
std::vector<std::unique_ptr<Base>> mappings;
};
class DerivedCollection: public Collection{
public:
DerivedCollection(std::string bla){
std::vector<std::unique_ptr<Base>> maps;
maps.push_back(std::make_unique<Derived>(bla));
//or this: (does not work aswell)
//maps.emplace_back(new Derived(bla));
this->mappings = maps;
}
};
int main(int argc, char** argv){
DerivedCollection test = DerivedCollection("bla");
return 0;
}
不知何故,仅定义 mappings
会触发错误:
/usr/include/c++/6.3.1/bits/stl_construct.h:75:7:
error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Base; _Dp = std::default_delete<Base>]’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
这告诉我,我设法从 const unique_ptr 构造了一个 unique_ptr,这不起作用,因为 unique_ptr 不可复制构造。
不知何故,即使我在 DerivedCollection
构造函数中注释所有内容,这仍然会失败。
我的猜测是我需要一个适合 Collection
class 的构造函数。不过我不确定如何定义它。
有什么想法吗?
-- 麦芽糖
maps
是不可复制的,因为它是 unique_ptr
的 vector
。将其移至 mappings
可解决问题:
this->mappings = std::move(maps);
您的代码还有其他问题:
你应该使用成员初始化列表来初始化数据成员而不是构造函数体.
getString
可以 returnconst std::string&
避免复制。Derived
的构造函数可以std::move
bla
进入数据成员。test
可以初始化为:DerivedCollection test{"bla"}
.
永远不要使用 new
- 请改用make_unique
。