I do not have an official answer, but I have an idea why this does not make much sense. The whole idea of unique_ptr is to be a strongly typed (i.e. compiler) way of having unique ownership of an object. Therefore, the semantics of displacement.
In this context, it makes sense that unique_ptr requires you to explicitly request your raw pointer, because as soon as you have the original value, you can easily break everything: you could delete it, you could pass it to another smart pointer ( which, in turn, will delete it at some point), or do pointer arithmetic and randomly work with the result (which may not indicate the allocated memory) or something like that. Some of them can immediately lead to compiler errors, others can lead to undefined behavior. For a high-level class such as unique_ptr , this is undesirable, at least it is implicit. Of course, you can do the same with .get() , but in this case you know that you are using the original value of the pointer and should not free it.
The link posted by Ami is also very relevant, but I think another example is better cited:
unique_ptr p( new widget ); ... use( p + 42 ); // error (maybe he meant "*p + 42"?) // but if implicit conversion to * were allowed, would silently compile -- urk
The philosophy of C ++ is that public functions that can work with this type belong to the interface of this type. Managing and restricting an interface of a type that is implicitly converted to a pointer is almost impossible. The intentionally restrictive nature of smart pointers will be completely deconstructed, resulting in code error.
Jonas wielicki
source share