Why are we using shared_ptr here? With unique_ptr The C++ version is trivially short. It's hard to reason about why the compiler does something in this case but I feel like the short version is that nobody would write this program, therefore nobody cares what the compiler does in this case.