Index: base/memory/scoped_ptr.h |
diff --git a/base/memory/scoped_ptr.h b/base/memory/scoped_ptr.h |
index 7133a3407d0a185158e1923ab961bc50f5e462e4..77a203992038f07687baad78a0b37ea55b21ba97 100644 |
--- a/base/memory/scoped_ptr.h |
+++ b/base/memory/scoped_ptr.h |
@@ -67,6 +67,22 @@ |
// the Pass() function to signify a destructive transfer of state. CreateFoo() |
// is different though because we are constructing a temporary on the return |
// line and thus can avoid needing to call Pass(). |
+// |
+// Pass() properly handles upcast in assignment, i.e. you can assign |
+// scoped_ptr<Child> to scoped_ptr<Parent>: |
+// |
+// scoped_ptr<Foo> foo(new Foo()); |
+// scoped_ptr<FooParent> parent = foo.Pass(); |
+// |
+// PassAs<>() should be used to upcast return value in return statement: |
+// |
+// scoped_ptr<Foo> CreateFoo() { |
+// scoped_ptr<FooChild> result(new FooChild()); |
+// return result.PassAs<Foo>(); |
+// } |
+// |
+// Note that PassAs<>() is implemented only for scoped_ptr, but not for |
+// scoped_array. This is because casting array pointers may not be safe. |
#ifndef BASE_MEMORY_SCOPED_PTR_H_ |
#define BASE_MEMORY_SCOPED_PTR_H_ |
@@ -182,6 +198,11 @@ class scoped_ptr { |
return retVal; |
} |
+ template <typename PassAsType> |
+ scoped_ptr<PassAsType> PassAs() { |
darin (slow to review)
2012/01/24 21:17:53
Had you considered putting this in base/move.h alo
Sergey Ulanov
2012/01/24 21:55:18
Yes, I considered it, but decided against it becau
|
+ return scoped_ptr<PassAsType>(release()); |
+ } |
+ |
private: |
C* ptr_; |