Prevent moving of a unique_ptr C++11
问题 Is there any way to prevent a user to explicity take ownership of a unique pointer with std::move ? 回答1: Make it const The unique_ptr move constructor takes a non-const rvalue reference, so can't be called with a const object. const unique_ptr<int> owner(new int); // ... unique_ptr<int> thief = std::move(owner); // ERROR This allows unique_ptr to be used like a boost::scoped_ptr 回答2: By returning a std::unique_ptr , you have given up the control of the object. The new owner will either