其他分享
首页 > 其他分享> > c – 在函数调用中访问并移动unique_ptr

c – 在函数调用中访问并移动unique_ptr

作者:互联网

我有一个类似于以下的部分.

struct derive : base{
    derive(unique_ptr ptr): base{func(ptr->some_data), std::move(ptr)}{}
};

从理论上讲,它应该有效.但由于编译器(vs2015)没有严格遵循标准,因此func(ptr-> some_data),std :: move(ptr)的顺序是未定义的,即ptr可以在访问之前移动.

所以我的问题是如何使这个细分市场按预期工作?

完整代码如下:

#include <memory>

struct base {
    virtual ~base() = 0 {}

protected:
    base(std::unique_ptr<base> new_state) :
        previous_state{ std::move(new_state) } {}
private:
    std::unique_ptr<base> previous_state;
};

struct derive_base : base {
    int get_a() const noexcept {
        return a;
    }
protected:
    derive_base(int const new_a, std::unique_ptr<base> new_state) :
        base{ std::move(new_state) }, a{ new_a } {}
private:
    int a;
};

struct final_state : derive_base {
    final_state(std::unique_ptr<base> new_state) :
        derive_base{ dynamic_cast<derive_base&>(*new_state).get_a(), std::move(new_state) } {}
};

解决方法:

你可以使用构造函数链来修复它:

struct derive : base
{
  private:
    derive(const D& some_data, unique_ptr<X>&& ptr) : base{some_data, std::move(ptr)} {}
  public:
    derive(unique_ptr<X> ptr): derive(func(ptr->some_data), std::move(ptr)) {}
};

原因:正如我在其他答案中所解释的那样,对func的调用肯定发生在委托构造函数调用之前,而实际移动unique_ptr(而不仅仅是更改其值类别)肯定发生在内部.

当然,这依赖于另一个C 11功能,Visual C可能会或可能没有正确.幸运的是,delegating constructors are listed as supported since VS2013.

更好的做法是始终通过引用接受std :: unique_ptr参数,如果您打算从它们中窃取,则通过右值引用接受. (如果您不会窃取内容,为什么要关心调用者具有哪种类型的智能指针?只需接受原始T *.)

如果你用过

struct base
{
    virtual ~base() = 0 {}

protected:
    base(std::unique_ptr<base>&& new_state) :
        previous_state{ std::move(new_state) } {}
private:
    std::unique_ptr<base> previous_state;
};

struct derive_base : base
{
    int get_a() const noexcept {
        return a;
    }
protected:
    derive_base(int const new_a, std::unique_ptr<base>&& new_state) :
        base{ std::move(new_state) }, a{ new_a } {}
private:
    int a;
};

struct final_state : derive_base
{
    final_state(std::unique_ptr<base>&& new_state) :
        derive_base{ dynamic_cast<derive_base&>(*new_state).get_a(), std::move(new_state) } {}
};

你不会在第一时间遇到问题,并且调用者要求完全不变(必须提供rvalue,因为unique_ptr无论如何都是不可复制的)

使其成为通用规则的基本原理如下:按值传递允许复制或移动,无论哪个在呼叫站点更优化.但是std :: unique_ptr是不可复制的,所以实际的参数必须是rvalue.

标签:c,unique-ptr
来源: https://codeday.me/bug/20190824/1711759.html