Although unique_ptr guaranteed to store nullptr after move, it still is pointing to the object?
问题 I've tested following code with GCC 5.2 (C++11): #include <iostream> #include <memory> struct Foo { Foo() { std::cout << "Foo::Foo\n"; } ~Foo() { std::cout << "Foo::~Foo\n"; } void bar() { std::cout << "Foo::bar\n"; } }; void f(const Foo &) { std::cout << "f(const Foo&)\n"; } int main() { std::unique_ptr<Foo> p1(new Foo); // p1 owns Foo if (p1) p1->bar(); { //p1->bar(); std::unique_ptr<Foo> p2(std::move(p1)); // now p2 owns Foo f(*p2); p1->bar(); if(p1==nullptr) { std::cout<<"NULL"<<std::endl