Why the code below outputs 1 1
rather than 2 1
? It seems that smart pointer initialized with this
in class does not increase use_count
#include <iostream>
#include <memory>
using namespace std;
struct T1 {
using tp1 = shared_ptr<T1>;
tp1 a;
T1() : a(this) {}
};
int main() {
auto x = make_shared<T1>();
cout << x.use_count() << endl;
x->a = nullptr;
cout << x.use_count() << endl;
}