Skip to content

Commit 08522ad

Browse files
committed
Simplify observer_ptr comparison
The comparison of _ptr is redundant as it can only be different if _auxiliary is different, and we don't care if it's the same unless _auxiliary is the same. Annoyingly, the same simplification can't apply to the other comparison operators. operator< will give a different order to other kinds of pointer comparison if it only cares about _auxiliary, and when comparing other kinds of pointer, we need to check that rhs isn't null before dereferencing it to get its auxiliary pointer, so still need three comparisons. We could do a different three comparisons, but this is the easiest combination to read that works.
1 parent 924d039 commit 08522ad

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

include/vsg/core/observer_ptr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ namespace vsg
100100
bool operator<(const observer_ptr<R>& rhs) const { return (rhs._ptr < _ptr) || (rhs._ptr == _ptr && rhs._auxiliary < _auxiliary); }
101101

102102
template<class R>
103-
bool operator==(const observer_ptr<R>& rhs) const { return (rhs._ptr == _ptr) && (rhs._auxiliary == _auxiliary); }
103+
bool operator==(const observer_ptr<R>& rhs) const { return (rhs._auxiliary == _auxiliary); }
104104

105105
template<class R>
106-
bool operator!=(const observer_ptr<R>& rhs) const { return (rhs._ptr != _ptr) || (rhs._auxiliary != _auxiliary); }
106+
bool operator!=(const observer_ptr<R>& rhs) const { return (rhs._auxiliary != _auxiliary); }
107107

108108
template<class R>
109109
bool operator<(const R* rhs) const

0 commit comments

Comments
 (0)