I have a class that I want to be able to construct with a temporary unique_ptr, like this:
MyCollection foo(std::unique_ptr<MyObj>(nullptr));
The object should take ownership of the pointer. My question is, what is the correct constructor signature for this?
1. MyCollection(std::unique_ptr<MyObj> foo);
2. MyCollection(std::unique_ptr<MyObj>&& foo);
The first option does not link. The second does, however what happens if I go with this, and then try to construct MyCollection with a non R-value? ie
std::unique_ptr<MyObj> pointer(nullptr);
MyCollection(pointer);
The answer here: How do I pass a unique_ptr argument to a constructor or a function? suggests that I should take the unique_ptr by value, but as I said above, it does not link in VS2010 (the error looks like this...
Error 5 error LNK2028: unresolved token (0A00075A) "private: __thiscall std::unique_ptr<class IVDSDocCore,struct std::default_delete<class IVDSDocCore> >::unique_ptr<class IVDSDocCore,struct std::default_delete<class IVDSDocCore> >(class std::unique_ptr<class IVDSDocCore,struct std::default_delete<class IVDSDocCore> > const &)" (??0?$unique_ptr@VIVDSDocCore@@U?$default_delete@VIVDSDocCore@@@std@@@std@@$$FAAE@ABV01@@Z) referenced in function "public: static void __clrcall std::unique_ptr<class IVDSDocCore,struct std::default_delete<class IVDSDocCore> >::<MarshalCopy>(class std::unique_ptr<class IVDSDocCore,struct std::default_delete<class IVDSDocCore> > *,class std::unique_ptr<class IVDSDocCore,struct std::default_delete<class IVDSDocCore> > *)" (?<MarshalCopy>@?$unique_ptr@VIVDSDocCore@@U?$default_delete@VIVDSDocCore@@@std@@@std@@$$FSMXPAV12@0@Z) C:\sviluppo\FerrariGes\GesDB\VDS.NET\VDS\vdsdoc.obj
Some replies suggest I need to use the move function. If I use constructor 1, and try to create the object like this:
If I use constructor 1, and try to create the object like this:
MyCollection foo(move(std::unique_ptr<MyObj>(nullptr)));
I get the same link error.