Copying std::unique_ptr's value via dereferencing

∥☆過路亽.° 提交于 2019-12-10 13:30:00

问题


I wrote the following code where I try to copy the value of unique_ptr object into a structure.

#include <iostream>
#include <memory>
using namespace std;

struct S {
    S(int X = 0, int Y = 0):x(X), y(Y){}

    // S(const S&) {}
    // S& operator=(const S&) { return *this; }

    int x;
    int y;
    std::unique_ptr<S> ptr;
};

int main() {
    S s;
    s.ptr = std::unique_ptr<S>(new S(1, 4));
    S p = *s.ptr; // Copy the pointer's value
    return 0;
}

It pops up errors in Visual C++ 2012:

IntelliSense: no suitable user-defined conversion from "S" to "S" exists
IntelliSense: no operator "=" matches these operands operand types are: std::unique_ptr> = std::unique_ptr>
error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'

Unless I uncomment the lines where I attempted to define a copy constructor and =operator. This gets rid of the compiler errors but not the IntelliSense errors. It compiles regardless of IntelliSense errors showing in error list.

So, why cannot it just use the default functions and compile with them? Am I doing the copy of value the right way? How should I define the copy constructor if it needs one?


回答1:


The copy constructor is no implicitly generate because you have a user defined constructor, why is why your attempt to copy an S fails.

But still, unique_ptr are not copyable, only movable, so you can use a move constructor for S :

S(S&& other) : x(other.x), y(other.y), ptr(std::move(other.ptr))
{

}

And call it :

S p = std::move(s); // Move s to p

Live demo




回答2:


std::unique_ptr is neither Copy Constructible nor Copy Assignable.

An implicit copy assignment operator and constructor for S will be ill formed and hence the error message.

You can however use S p = std::move(s); as std::unique_ptr is Move Constructible and Move Assignable,




回答3:


Not a complete answer, just informational:

I highly recommend adding visibility into your experiment:

std::ostream&
operator<<(std::ostream& os, const S& s)
{
    os << '{' << s.x << ", " << s.y << ", ";
    if (s.ptr != nullptr)
        os << s.ptr.get() << ':' << *s.ptr;
    else
        os << "nullptr";
    return os << '}';
}

Now you can say things like:

cout << "s = " << s << '\n';

at multiple places in your experiment, and really get a good visual on what is happening after each step. This should help you analyze and continue in your design.




回答4:


So, why cannot it just use the default functions and compile with them?

As far as I understand, the idea behind unique_ptr container is that it solely handles the life of its content (a pointer to T), until being relieved from that duty (using swap or reset methods), or having effectively destroyed its content (when it is itself destroyed). The second important property of unique_ptr is that it must allow incomplete types for T (so as to support opaque pointers). That means that the contained value may not be CopyConstructible. Because of this, unique_ptr itself cannot be allowed to be CopyConstructible.

Am I doing the copy of value the right way? How should I define the copy constructor if it needs one?

If T ends up being CopyConstructible, as you want to do it, you must handle the copy by hand, by accessing the pointer, as you are doing it in main. The copy constructor should probably do the same thing.



来源:https://stackoverflow.com/questions/26573972/copying-stdunique-ptrs-value-via-dereferencing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!