Why can't a pointer be automatically converted into a unique_ptr when returning it?
问题 Let me pose my question through an example. #include <memory> std::unique_ptr<int> get_it() { auto p = new int; return p; } int main() { auto up ( get_it() ); return 0; } This fails to compile with the following error: a.cpp:5:9: error: could not convert ‘p’ from ‘int*’ to ‘std::unique_ptr<int>’ return p; ^ Why isn't there an automatic conversion from a raw pointer to a unique one here? And what should I be doing instead? Motivation: I understand it's supposed to be good practice to use smart