unique_ptr类型的Vector,继承
vector of unique_ptr, inheritance?
假设代码如下:
class Parent {}
class Child : public Parent {}
static std::vector<std::unique_ptr<Child>> Foo();
有没有更简单的方法来写这个函数:
std::vector<std::unique_ptr<Parent>> Bar() {
auto children = Foo();
std::vector<std::unique_ptr<Parent>> parents;
result.insert(result.end(), std::make_move_iterator(children.begin()),
std::make_move_iterator(children.end()));
return parents;
}
这行不通:
std::vector<std::unique_ptr<Parent>> Bar() {
return Foo(); // Compiler error: cannot convert from vector<...> to vector<...>
}
类型不同。Foo()
返回std::vector<std::unique_ptr<Child>>
, Bar()
返回std::vector<std::unique_ptr<Parent>>
。这是无法规避的。然而,与其:
std::vector<std::unique_ptr<Parent>> Bar() {
auto children = Foo();
std::vector<std::unique_ptr<Parent>> parents;
result.insert(result.end(), std::make_move_iterator(children.begin()),
std::make_move_iterator(children.end()));
return parents;
}
你可以这样做:
std::vector<std::unique_ptr<Parent>> Bar() {
auto tmp = Foo();
return {std::make_move_iterator(tmp.begin()), std::make_move_iterator(tmp.end()));}
}
我们可以在其他地方写样板文件:
templace<class Cin>
struct move_from_c{
Cin* c;
template<class Cout>
operator Cout()&&{
using std::begin; using std::end;
return {std::make_move_iterator(begin(*c)), std::make_move_iterator(end(*c))};
}
};
template<class C, class dC=std::remove_reference_t<C>>
move_from_c<dC> move_from(C&&c){
return {std::addressof(c)};
}
你的函数是:
std::vector<std::unique_ptr<Parent>> Bar() {
return move_from(Foo());
}
这将Bar
的实现细节与业务逻辑分开。(搬离的方式和搬离的决定是分开的)
几个"更简单"的方法:
#include <algorithm>
#include <iterator>
std::vector<std::unique_ptr<Base>> f1(std::vector<std::unique_ptr<Derived>> v)
{
std::vector<std::unique_ptr<Base>> result;
result.reserve(v.size());
std::move(v.begin(), v.end(), std::back_inserter(result));
return result;
}
std::vector<std::unique_ptr<Base>> f2(std::vector<std::unique_ptr<Derived>> v)
{
return {std::make_move_iterator(v.begin()), std::make_move_iterator(v.end())};
}
建议使用句柄/句体习语,并将多态性作为句柄类的实现细节来实现。
这为您提供了值语义(适用于容器),并且还允许您轻松实现关系操作符:
#include <vector>
#include <memory>
class ParentImpl {
public:
virtual ~ParentImpl() = default;
virtual void foo() {}
};
class ChildImpl : public ParentImpl {};
class ParentHandle
{
public:
using ptr_type = std::unique_ptr<ParentImpl>;
// construct from ptr_type
ParentHandle(ptr_type ptr = nullptr) : ptr_(std::move(ptr_)) {}
// public interface defers to polymorphic implementation
void foo()
{
ptr_->foo();
}
private:
std::unique_ptr<ParentImpl> ptr_;
};
static std::vector<ParentHandle> Foo()
{
std::vector<ParentHandle> result;
result.emplace_back(std::make_unique<ParentImpl>());
result.emplace_back(std::make_unique<ChildImpl>());
return result;
}
相关文章:
- C++ std::vector 中的虚拟析构函数继承
- 如何创建一个继承自 std::vector 的类
- 无法在从std :: vector继承的类中打字迭代器
- 在C++中扩展容器(如std::vector)功能的正确方法,而不从中继承
- 为自定义类继承std::vector::迭代器
- 错误:当我继承 std::vector 时,在“开始”之前预期“)”
- 如何在 C++11 的用户自定义类模板中继承 std::vector 模板
- 这个从 std::vector 继承的类声明有问题吗?
- 继承自 std::vector<T> 和重载运算符 [] 用于自定义索引
- 使用继承c++在vector中插入一个模板对象
- 如何在继承boost::any中包含的std::vector的类上循环
- 调用2类继承的vector函数
- 在继承std::vector的类中使用操作符[]
- 在c++中继承vector类时重载方括号
- 继承Vector: Vector基类
- unique_ptr类型的Vector,继承
- 修改std::vector函数(继承?)
- vector +继承的用法
- 将类模板继承的类存储在C++Vector中
- 我如何从mpl::vector中的所有类型继承