I have the following minimal reproducible example where it seems that my unique-pointer-containing object B is not allowed to contain a unique pointer to another instance of B:
#include <iostream>
#include <memory>
struct C {
virtual void say_name() = 0;
};
struct A : public C {
int value;
A(int value) : value(value) {}
void say_name() override { std::cout << "A" << value << std::endl; }
};
struct B : public C {
std::unique_ptr<C> ptr;
B(std::unique_ptr<C> ptr) : ptr(std::move(ptr)) {}
void say_name() override { std::cout << "B" << std::endl; }
};
int main() {
A a(5);
B b(std::make_unique<A>(10));
// Here I create another B - causes error
B b2(std::make_unique<B>(b));
}
I expected the following to happen:
I expected b to own a, and b2 to own b which owns a.
However, I get an error message when I try and create b2 that says I have a call to an implicitly deleted copy-constructor of B.
I don't understand the error or how to fix it and want to understand how I should restructure my program so that I can nest objects in this way.
Here is the full error message:
In template: call to implicitly-deleted copy constructor of 'B'clang(ovl_deleted_special_init)
unique_ptr.h(767, 30): Error occurred here
main.cpp(33, 13): In instantiation of function template specialization 'std::make_unique<B, B &, 0>' requested here
main.cpp(24, 22): Copy constructor of 'B' is implicitly deleted because field 'ptr' has a deleted copy constructor
unique_ptr.h(221, 55): Copy constructor is implicitly deleted because 'unique_ptr<C>' has a user-declared move constructor
std::make_unique<B>(b)attempts to call the copy-constructor ofBwhich is deleted because of thestd::unique_ptrmember which is non-copyable.Copy constructor of 'B' is implicitly deleted because field 'ptr' has a deleted copy constructor. C++ doesn't get much clearer than this.