Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2957)

Unified Diff: base/memory/ref_counted.h

Issue 1963323004: Use explicit operator bool in scoped_refptr. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@explicit-operator-bool-ref-counted-
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/ref_counted.h
diff --git a/base/memory/ref_counted.h b/base/memory/ref_counted.h
index 0c0e0afd770f4bb3f71d222b098d200c4e3688d4..01da9b1e245d747051d8d4b5e613b3b0bf7bc3ae 100644
--- a/base/memory/ref_counted.h
+++ b/base/memory/ref_counted.h
@@ -362,30 +362,7 @@ class scoped_refptr {
swap(&r.ptr_);
}
- private:
- template <typename U> friend class scoped_refptr;
-
- // Implement "Safe Bool Idiom"
- // https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool
- //
- // Allow scoped_refptr<T> to be used in boolean expressions such as
- // if (ref_ptr_instance)
- // But do not become convertible to a real bool (which is dangerous).
- // Implementation requires:
- // typedef Testable
- // operator Testable() const
- // operator==
- // operator!=
- //
- // == and != operators must be declared explicitly or dissallowed, as
- // otherwise "ptr1 == ptr2" will compile but do the wrong thing (i.e., convert
- // to Testable and then do the comparison).
- //
- // C++11 provides for "explicit operator bool()", however it is currently
- // banned due to MSVS2013. https://chromium-cpp.appspot.com/#core-blacklist
- typedef T* scoped_refptr::*Testable;
- public:
- operator Testable() const { return ptr_ ? &scoped_refptr::ptr_ : nullptr; }
+ explicit operator bool() const { return ptr_ != nullptr; }
template <typename U>
bool operator==(const scoped_refptr<U>& rhs) const {
@@ -406,6 +383,10 @@ class scoped_refptr {
T* ptr_;
private:
+ // Friend required for move constructors that set r.ptr_ to null.
+ template <typename U>
+ friend class scoped_refptr;
+
// Non-inline helpers to allow:
// class Opaque;
// extern template class scoped_refptr<Opaque>;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698