Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valid situations for comparing shared_ptr instances

Under what situations would it be valid to compare shared_ptr instances instead of the underly lying type the shared_ptr manages?

As an example, would there ever be a situation where the size of personset being 2 would be valid after the following code has run?

shared_ptr<person> p0 = make_shared<person>(....);
shared_ptr<person> p1 = p0;

set<shared_ptr<person>> personset;

personset.insert(p0);
personset.insert(p1);
like image 292
Jacobi John Avatar asked Oct 19 '25 04:10

Jacobi John


1 Answers

There is no viable reason to compare the instances. Infact shared_ptrs by default will perform equality/inequality comparators based via the underlying pointer to the control block (via .get method).

http://en.cppreference.com/w/cpp/memory/shared_ptr/operator_cmp

like image 137
Zamfir Kerlukson Avatar answered Oct 22 '25 01:10

Zamfir Kerlukson