【问题标题】:Use copy constructor while user-declared move constructor is present存在用户声明的移动构造函数时使用复制构造函数
【发布时间】:2020-12-12 01:22:35
【问题描述】:

在处理unique_ptr 时,我对使用复制构造函数和复制赋值运算符有些困惑。如果您能分享一些见解,我将不胜感激!

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

啊.h

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;

B.h

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&lt;A&gt; 具有用户声明 移动构造函数?

【问题讨论】:

  • 如果您需要一个类似unique_ptr 的提供复制功能的包装器,可以从头开始编写一个(或创建一个包含std::unique_ptr 但通过用户定义的复制ctor/ 提供复制的类赋值运算符)。让它在所有与unique_ptr 相同的情况下工作是很棘手的,但如果不需要 - 没问题。

标签: c++ class copy-constructor copy-assignment


【解决方案1】:

让我删除一些格式:

 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 没有复制构造函数,因为它是唯一的。它不能被复制。因此,包含std::unique_ptr 的任何内容的默认复制构造函数都将被删除。所以你的 C 类没有复制构造函数。另外,你从来没有给它一个移动构造函数。

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

所以解决方案是给C 一个工作拷贝构造函数和/或一个移动构造函数。如果它对你的C 是有意义的,你可以通过对指向A 进行深层复制来给它一个复制构造函数,但我不知道A 是什么,所以我不能说对于某些。不过,创建移动构造函数几乎总是有意义的。

【讨论】:

    【解决方案2】:
    template<typename X, typename D=std::default_deleter<A>, typename base=std::unique_ptr<A,D>>
    struct not_unique_ptr
    :   base {
        static_assert(std::is_same_v<base,std::unique_ptr<A,D>>);
        not_unique_ptr()=default;
        not_unique_ptr(not_unique_ptr &&) = default;
        not_unique_ptr(not_unique_ptr const& init)
        :   base{std::make_unique<A>(*init)};
        explicit not_unique_ptr(base && b) 
        :   base{std::move(b)}{};
        auto& operator=(not_unique_ptr &&) = default;
        auto& operator=(base&& rhs){
            base&{*this}=std::move(rhs);
        };
        auto& operator=(not_unique_ptr const& rhs){
            base&{*this}=base&&{not_unique_ptr{rhs}};
        };
    };
    

    【讨论】:

      猜你喜欢
      • 2012-10-10
      • 1970-01-01
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 2012-10-10
      • 1970-01-01
      • 2021-12-25
      • 2019-03-09
      相关资源
      最近更新 更多