The following code doesn't compile -
struct A {
A() = default;
A(const A& other) = delete;
};
int main()
{
auto a = A();
auto u = std::make_unique<A>(std::move(a));
}
While the following does -
struct A {
A() = default;
A(const A& other) = delete;
A(A&& other) = default;
};
int main()
{
auto u = std::make_unique<A>(A());
}
The error I got is call to implicitly-deleted copy constructor
.
Im using a blaze compiler for cpp-17.
Why does the first code segment not compile? It shouldn't use the copy contractor, just the move one.
Edit:
Adding A(A&& other) = default;
solves the issue.
Does this mean that deleting the copy-contractor also deletes the move-contractor implicitly, and it needs to be added?
CodePudding user response:
By deleting the copy constructor, you also implicitly delete the move constructor (A(A&& other)
). If you want to use the first code segment, you must define your own custom move constructor (which will depend on the actual data of A
).
Look at the concepts "rule of 5", "rule of 0" and related links (e.g. here) to get a grip on which constructors must be defined.
(by the way, the second code block does not compile on GCC or Clang, as pointed out in the comments. See godbolt)