2

The fact that unique_ptr doesn't allow copy which means no more than one object can share the same underlying data. If I contain an object of unique_ptr in my class will the compiler be wise enough to make the implicit copy constructor a deleted function?

class Blob {
    public:
        Blob(){}
    //  Blob(const Blob& rhs){}
        unique_ptr<int> upi{make_unique<int>(0)};
};

int main(int argc, char* argv[]) {

    Blob b;
    cout << *b.upi << endl;

    Blob b2(b); // error: Error C2280   'Blob::Blob(const Blob &)': attempting to reference a deleted function

}

If I un-comment the definition of the user-defined copy constructor then everything is alright as long as no copying of the unique_ptr there.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Itachi Uchiwa
  • 3,044
  • 12
  • 26
  • 1
    @user3365922 I think there's no doubt in OP:s mind about that. The post starts with "_The fact that unique_ptr doesn't allow copy ..._" – Ted Lyngmo Oct 24 '19 at 13:44

0 Answers0