cpx
cpx

Reputation: 17557

Copying std::unique_ptr's value via dereferencing

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?

Upvotes: 7

Views: 10355

Answers (4)

quantdev
quantdev

Reputation: 23793

The copy constructor is not implicitly generated because you have a user-defined constructor, which 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

Upvotes: 6

didierc
didierc

Reputation: 14730

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.

Upvotes: 2

Howard Hinnant
Howard Hinnant

Reputation: 218700

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.

Upvotes: 2

P0W
P0W

Reputation: 47784

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,

Upvotes: 2

Related Questions