Why is raw pointer to shared_ptr construction allowed in all cases?
Pointers are very easy to copy. Even if you restrict to r-value reference you can sill easily make copies (like when you pass a pointer as a function parameter) which will invalidate the safety setup. Moreover you will run into problems in templates where you can easily have T* const
or T*&
as a type and you get type mismatches.
So you are proposing to create more restrictions without significant safety gains, which is likely why it was not in the standard to begin with.
The point of make_shared
is to atomize the construction of a shared pointer. Say you have f(shared_ptr<int>(new int(5)), throw_some_exception())
. The order of parameter invokation is not guaranteed by the standard. The compiler is allowed to create a new int, run throw_some_exception
and then construct the shared_ptr which means that you could leak the int (if throw_some_exception
actually throws an exception). make_shared
just creates the object and the shared pointer inside itself, which doesn't allow the compiler to change the order, so it becomes safe.