Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

std move result still using copy-contractor

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading