mardi 20 février 2018

C++ shared+ptr use_count for nullptr

I have 2 shared_ptrs defined and assigned nullptr. In case 1, I'm using default constructor and in case 2, I used constructor with delete method.

shared_ptr<int> sptr2(nullptr);

cout << "sptr2 use_count: " << sptr2.use_count() << endl;

shared_ptr<int> sptr6(nullptr, default_delete<int>());

cout << "sptr6 use_count: " << sptr6.use_count() << endl;

The output is:

sptr2 use_count: 0
sptr6 use_count: 1

I cannot understand why sptr6 has use count of 1 when it does not have any valid pointer.

g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)

Aucun commentaire:

Enregistrer un commentaire