mardi 8 mai 2018

Returning an object that contains a unique_ptr does not seem to work using std::move()

I have a class something like:

test {
   std::unique_ptr<int> p_test;
   testclass() : p_test(nullptr) {p_test = make_unique<int>(1);}
};

Then I have a factory class for test objects:

test_factory {
    test create_test() {
        return std::move(test());
    };
};

Finally I some code that uses it:

int main() {
    test_factory f;
    test t = f.create_test();
    return 0;
}

I seem to be getting errors with the unique_ptr: error C2248 std::unique_ptr <_Ty>::unique_ptr : cannot access private member declared in class...

So I added the std::move(...) in the return value of the factory, but I still get the same error... I am not quite sure what is going wrong, but also this is probably the first time I have tried to pass an object containing a unique_ptr via "copy" (move)... so its likely that I have got something wrong here...

Aucun commentaire:

Enregistrer un commentaire