diff --git a/ctl/shared_ptr.h b/ctl/shared_ptr.h index 019d6fb29..ae3316831 100644 --- a/ctl/shared_ptr.h +++ b/ctl/shared_ptr.h @@ -335,11 +335,10 @@ class shared_ptr return p; } -#if 0 // TODO(mrdomino): find a different way template bool owner_before(const shared_ptr& r) const noexcept { - return p < r.p; + return rc < r.rc; } template @@ -347,7 +346,6 @@ class shared_ptr { return !r.owner_before(*this); } -#endif private: template @@ -422,13 +420,13 @@ class weak_ptr template bool owner_before(const weak_ptr& r) const noexcept { - return p < r.p; + return rc < r.rc; } template bool owner_before(const shared_ptr& r) const noexcept { - return p < r.p; + return rc < r.rc; } private: diff --git a/test/ctl/shared_ptr_test.cc b/test/ctl/shared_ptr_test.cc index 27dd0b76c..960d6b5fc 100644 --- a/test/ctl/shared_ptr_test.cc +++ b/test/ctl/shared_ptr_test.cc @@ -69,7 +69,7 @@ struct Derived : Base int main() { - int a; + int a, b; { // Shouldn't cause memory leaks. @@ -182,17 +182,16 @@ main() return 13; } -#if 0 // TODO(mrdomino): find a different way { - // owner_before works across shared and weak pointers. + // owner_before shows equivalence only for equivalent objects. shared_ptr x(&a, CallG()); shared_ptr y(&b, CallG()); - if (!x.owner_before(y)) + shared_ptr z(x, &b); + if (z.owner_before(x) || x.owner_before(z)) return 14; - if (!x.owner_before(weak_ptr(y))) + if (!z.owner_before(y) && !y.owner_before(z)) return 15; } -#endif { // Use counts work like you'd expect