Benefits:
- Clear expression of intent, i.e., “the assigning variable is not used anymore”.
- Reduce unnecessary reference counter updates. Micro benchmark
Example:
{
scoped_refptr<Foo> local_ptr;
...
member_ptr_ = local_ptr;
...
}
This would call a copy assignment operator, and |local_ptr|’s destructor at the end of the scope, that increments and decrements the reference counter. Now we can do this instead.
member_ptr_ = local_ptr.Pass();
Then a move assignment operator is called: |member_ptr_| will have |local_ptr|’s pointer and |local_ptr| will be |nullptr|. And this doesn’t involve reference counter updates. This appears to be particularly efficient when dealing with |scoped_refptrs| to |ThreadSafeRefCounted| objects, which has relatively more expensive reference counter updates.
Notes:
Thanks to dcheng@, danakj@, mgiuca@, rsleevi@, and trchen@ for reviewing and help completing the CL.
Kibeom