首页 > 解决方案 > 存在用户声明的移动构造函数时使用复制构造函数

问题描述

在处理unique_ptr. 如果您能分享一些见解,我将不胜感激!

现在我们上课了B
结构C是类的成员B
结构C有一个std::unique_ptr<A>成员。

class A {
 public:
  A(int* id);
  A(const A& other);
  ~A() override;

 private:
  Microsoft::WRL::ComPtr<int> id_;
};

A.cpp

A::A(int* id) : id_(id) {
}

A::A(
    const A& other) {
  id_ = other.id_;
}

A::~A() = default;

溴化氢

class B {
 public:
  struct C{
   public:
    C(
        int input_pad_id,
        Microsoft::WRL::ComPtr<int> input_id,
        std::unique_ptr<A>& input_a);
    C(const C& other);
    C& operator=(const C&);
    ~C();

    int pad_id;
    Microsoft::WRL::ComPtr<int> id;
    std::unique_ptr<A> a;
  };

  B();
  B(const B&) = delete;
  B& operator=(const B&) = delete;
  ~B() override;

 private:
  std::vector<C> c_item_;
};

B.cpp

B::B() = default;

B::~B() = default;

/* omitting some code related to operation logic */
...
c_item_.push_back({pad_id, id, nullptr});
...
/* omitting some code related to operation logic */

B::C::C(int input_pad_id,
        Microsoft::WRL::ComPtr<int> input_id,
        std::unique_ptr<A>& input_a)
    : pad_id(input_pad_id), id(input_id),
      a(std::move(input_a)) {}

B::C::~C() = default;

B::C::C(const C& other) = default;

B::C& B::C::operator=(const B::C& other) = default;

在构建时,我得到了这个错误日志:

error: no matching member function for call to 'push_back'
  c_item_.push_back({pad_id, id, nullptr});
  ~~~~~~~~~~^~~~~~~~~
../../buildtools/third_party/libc++/trunk/include\vector(711,36): note: candidate function not viable: cannot convert initializer list argument to 'const std::__vector_base<B::C, std::allocator<B::C>>::value_type' (aka 'const B::C')
    _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
                                   ^
../../buildtools/third_party/libc++/trunk/include\vector(714,36): note: candidate function not viable: cannot convert initializer list argument to 'std::vector<B::C>::value_type' (aka 'B::C')
    _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
                                   ^
../B.cc(311,5): error: defaulting this copy constructor would delete it after its first declaration
    B::C::C(const C& other) = default;
       ^
../B.h(33,46): note: copy constructor of 'C' is implicitly deleted because field 'c_item' has a deleted copy constructor
    std::unique_ptr<C> c_item;

../../buildtools/third_party/libc++/trunk/include\memory(2528,3): note: copy constructor is implicitly deleted because 'unique_ptr<B>' has a user-declared move constructor
  unique_ptr(unique_ptr&& __u) _NOEXCEPT
  ^
../B.cc(315,65): error: defaulting this copy assignment operator would delete it after its first declaration
    B::C& B::C::operator=(const B::C& other) = default;
                ^
../B.h(33,46): note: copy assignment operator of 'C' is implicitly deleted because field 'c_item' has a deleted copy assignment operator
    std::unique_ptr<A> a;

../../buildtools/third_party/libc++/trunk/include\memory(2528,3): note: copy assignment operator is implicitly deleted because 'unique_ptr<A>' has a user-declared move constructor
  unique_ptr(unique_ptr&& __u) _NOEXCEPT
  ^

在这种情况下,如何通过使代码使用unique_ptr<A>具有用户声明的移动构造函数时可用的复制构造函数来解决此编译错误?

标签: c++classcopy-constructorcopy-assignment

解决方案


让我删除一些格式:

 error: defaulting this copy constructor would delete it...
  C(const C& other) = default;
 ...because copy constructor of 'C' is implicitly deleted because field 'c_item' has a deleted copy constructor
  std::unique_ptr<C> c_item;

std::unique_ptr没有复制构造函数,因为它是唯一的。它不能被复制。因此,包含 a 的任何内容的默认复制构造函数都将std::unique_ptr被删除。所以你的C类没有复制构造函数。此外,您从未给它一个移动构造函数。

并且std::vector插入方法(又名 push_back)可能必须调整大小,因此它必须移动或复制其元素,但C没有复制或移动构造函数,因此您会遇到编译器错误。

所以解决方案是提供C一个工作副本构造函数和/或一个移动构造函数。如果它对你的任何东西都有意义C,你可以通过制作指向的深层副本来给它一个复制构造函数A,但我不知道是什么A,所以我不能肯定地说。不过,创建移动构造函数几乎总是有意义的。


推荐阅读