Chromium Code Reviews| Index: base/memory/ref_counted.h |
| diff --git a/base/memory/ref_counted.h b/base/memory/ref_counted.h |
| index 0fb7447b999e744e39ff24336005bcbd593172eb..490ef3119b660841ab7a2813bb3f5247d4aa4359 100644 |
| --- a/base/memory/ref_counted.h |
| +++ b/base/memory/ref_counted.h |
| @@ -333,6 +333,18 @@ 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; |
|
Lei Zhang
2014/11/26 00:44:18
nit: blank line after.
dcheng
2014/11/26 00:45:28
Done.
|
| + 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(); |