No, absolutely not. You will get undefined behavior since the raw pointer controlled by unique_ptr changes to reset . In fact, unique_ptr removes the managed pointer and redistributes the new one, so your old pointer will point to the old address, which is no longer valid. Your code is equivalent to this:
#include <iostream> int main() { int* managed = new int(51); // equivalent to unique_ptr(new int(51)); int* bar = managed; // equivalent of unique_ptr::get delete managed; managed = new int(3); // equivalent to unique_ptr::reset(new int(3)) std::cout << *bar << std::endl; // Wrong!!! Undefined behaviour, bar is dangling! }
Your first piece of code is really correct, since you are not changing any address there, you are just changing the variable that the pointer points to, so disclosing the pointer will give you an updated value.
source share