Index: base/memory/ref_counted.h |
diff --git a/base/memory/ref_counted.h b/base/memory/ref_counted.h |
index 0fb7447b999e744e39ff24336005bcbd593172eb..219437ed8d82b8b35835a6c3297d5f719f14c277 100644 |
--- a/base/memory/ref_counted.h |
+++ b/base/memory/ref_counted.h |
@@ -333,6 +333,19 @@ class scoped_refptr { |
swap(&r.ptr_); |
} |
+ private: |
+ // Allow scoped_refptr<T> to be used in boolean expressions, but not |
+ // implicitly convertible to a real bool (which is dangerous). |
+ // |
+ // Note that this trick is only safe when the == and != operators |
+ // are declared explicitly, as otherwise "refptr1 == refptr2" |
+ // will compile but do the wrong thing (i.e., convert to Testable |
+ // and then do the comparison). |
+ typedef T* scoped_refptr::*Testable; |
+ |
+ public: |
+ operator Testable() const { return ptr_ ? &scoped_refptr::ptr_ : nullptr; } |
+ |
template <typename U> |
bool operator==(const scoped_refptr<U>& rhs) const { |
return ptr_ == rhs.get(); |