I don't understand why this code works:
class Base {
public:
virtual void doStuff() const{}
Base(Base & b) = delete;
Base() {
cout << "Base" << endl;
}
~Base() {
cout << "~Base" << endl;
}
friend ostream& operator<<(ostream& out, const Base& a) {
return out << "me";
}
};
int main(){
unique_ptr<Base> pt = make_unique<Base>();
auto npt = move(pt);
auto &a = *pt;
if (pt == nullptr)
cout << "yes nullptr" << endl;
cout << a << endl;
}
The output in Visual Studio 2015 is:
Base
yes nullptr
me
~Base
So it doesn't crash and pt
is even usable after being moved from.
In coliru online compiler, it crashes at line cout << a << endl;
. I don't understand how it doesn't crash at line auto &a = *pt;
, since pt is equal to nullptr at this point and the command auto &refToNull= nullptr;
is a compilation error.
I will appreciate a clarification about what's going on.