by pcwalton · 12y ago · 0 comments
Sure, other than not actually being safe (UB resulting from use after move of unique_ptr being an obvious one; there are many others).
The lesson of C++ is that you can't bolt on safety by using templates to paper over the underlying mess. The mold always seeps through the wallpaper. The usual problem is that at some point you need raw pointers to call some API. If you can get raw pointers, the abstraction has to have a leak.
Nope, the argument is "valid but unspecified" not necessarily null. And http://en.cppreference.com/w/cpp/utility/move specifically has this as an example of UB:
std::vector<std::string> v;
std::string str = "example";
v.push_back(std::move(str)); // str is now valid but unspecified
str[0]; // undefined behavior: operator[](size_t n) has a precondition size() > n
There's an other fun UB because std::move doesn't check for aliasing, so self-assignment: v = std::move(v);
is also an UB.And of course even if it were null., pcwalton talks about use after null. Deref'ing a null pointer (to use it) is still an UB.
From 20.7.1
Additionally, u can, upon request, transfer ownership to
another unique pointer u2. Upon completion of such a
transfer, the following postconditions hold:
— u.p is equal to nullptr,
unique_ptr isn't what's unsafe here. Null pointers are. The difference, admittedly, is mostly semantic. There is nothing undefined about using a unique_ptr after moving unless you use the internal pointer in undefined ways.If you dereference (i.e. use) a moved unique pointer you get undefined behavior.
When talking about "using a pointer" most people mean dereferencing it, not for example comparing against null. It may be somewhat imprecise language, but it's what security people mean when they talk about "use after free", for example.