Creating const unique_ptr and then trying std :: move from gives the same error as if you were trying to access the copy constructor

I noticed an error while trying to copy unique_ptr(for example, assign one unique pointer to another)

Error C2280 std::unique_ptr<int,std::default_delete attempting to reference a deleted function ptrTest c:\ptrtest\main.cpp 7
#include <memory>

int main()
{
    std::unique_ptr<int> a = std::make_unique<int>(2);
    std::unique_ptr<int> b = a;
}

This is great because it unique_ptrdoes not have a specific copy constructor. You do not copy from unique pointers to move (transfer ownership of the pointer) between them.

Interesting (OK, maybe not), this code causes the same error. Now I know that it is invalid (I declared the first unique_ptras an immutable object), but the error message means that it is trying to call the copy constructor. It's right?

#include <memory>

int main()
{
    const std::unique_ptr<int> a = std::make_unique<int>(2);
    std::unique_ptr<int> b = std::move(a);
}
+4
1

.

- , & hellip; , , . .

++, , , - . .

+7

Source: https://habr.com/ru/post/1668325/


All Articles