#include <memory>
#include <iostream>
int
main(int argc, char** argv) {
std::shared_ptr<int> p(new int);
std::shared_ptr<int> p2(p);
std::cout << p.use_count() << std::endl;
return 0;
}
output: 2
EXPLANATION/EDIT: In your source, the initial 'p' never held ownership of anything. In the 2nd reference to p, you are assigning to a temporary and basically giving up ownership to 'p'. Most likely, as well, the move constructor is used to satisfy this assignment.
EDIT: This was probably what you were going for?
#include <memory>
#include <iostream>
int
main(int argc, char** argv) {
std::shared_ptr<int> p(new int);
{
std::shared_ptr<int> p2(p);
std::cout << p.use_count() << std::endl;
}
std::cout << p.use_count() << std::endl;
return 0;
}
output: 2
1