【问题标题】:C++11 converting a unique_ptr to a base classC++11 将 unique_ptr 转换为基类
【发布时间】:2016-04-12 19:44:06
【问题描述】:

我有一个函数,它接受一些对象向量并对其进行过滤,并且需要返回转换为基类的过滤后的对象向量。

class Base {
    // stuff
}

class Derived : public Base {
    // more stuff
}

// elsewhere...
vector<reference_wrapper<const unique_ptr<Base>>> do_something(const vector<unique_ptr<Derived>> &things) {
    vector<reference_wrapper<const unique_ptr<Base>>> a;

    for (const unique_ptr<Derived> &derived : things) {
        if (check(derived)) {
            a.push_back(derived);  // this obviously doens't work -
                                   // what should I put here?!

            // this does not work:
            // a.push_back(std::ref(derived.get()))
            // because derived.get() is an rvalue
        }
    }

    return a;
}

返回的向量不拥有任何原始对象 - 它们应该只是阅读器。原始对象的寿命将超过返回向量的寿命。

【问题讨论】:

  • 你确定要vector&lt;reference_wrapper&lt;const unique_ptr&lt;Base&gt;&gt;&gt;吗?
  • 除非返回的向量应该拥有对象,否则您应该只返回一个原始指针向量,而不是unique_ptrs。据我所知,things 应该保留对象的所有权。
  • @JoelCornett 这是个好主意吗?消费者不能随意“删除”指针吗?
  • @Charles 如果这是一个真正的问题,那么您应该将指针包装在一个自定义类中,以防止用户删除这些对象。您也可能希望通过shared_ptr 共享所有权。除非您将对象的所有权转移给调用者,否则您确实想要unique_ptr
  • @Charles 在新世界中,原始指针应该被视为由其他人拥有,任何人都不应该删除它们(除非他们碰巧正在编写 smart指针 或者,也许是一个容器类)。在大多数情况下,拨打newdelete 应该成为过去。

标签: c++ unique-ptr ownership


【解决方案1】:

我还没有阅读 cmets,所以也许它已经在那里回答了。但是,通常,当向量不拥有任何指向的成员时,不会使用对 unique_ptr 的引用——正如这所表达的,向量应该能够仅使用 unique_ptr 做某些事情楼主可以。

完整观察者的常用方法是使用原始指针(至少在observer_ptr 出现在标准库中之前)。约束是你永远不能在这些指针上调用delete 或任何其他与内存相关的操作(这是observer_ptr 在设计上不允许的)。

所以你写:

auto do_something(vector<unique_ptr<Derived>> const&things)
{
    vector<Base *> a;

    for (auto& derived : things) {
        if (check(derived))
        {
            a.push_back(derived.get());
        }

    return a;
}

与您选择引用const unique_ptr 的情况进行简短比较:unique_ptr interface 中的可调用const 函数由get()get_deleter()operator bool() 给出。在这三个中,使用vector&lt;Base *&gt; 方法,您失去了调用get_deleter() 的可能性。然而,作为一个读者,几乎可以肯定不需要删除器。

Scott Meyer(和他的参考资料)给出了一个稍微相关的讨论,这里可能会引起您的兴趣:http://scottmeyers.blogspot.de/2014/07/should-move-only-types-ever-be-passed.html

【讨论】:

    猜你喜欢
    • 2021-02-18
    • 2016-01-02
    • 2019-07-10
    • 2016-10-19
    • 2013-05-08
    • 2018-12-15
    • 2021-08-19
    • 1970-01-01
    • 2014-03-27
    相关资源
    最近更新 更多