mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-07 18:04:46 +00:00
Bug 1710145 - Deduction guide RefPtr(already_AddRefed<T>) -> RefPtr<T> - r=kmag
Differential Revision: https://phabricator.services.mozilla.com/D114809
This commit is contained in:
parent
e726c98cf2
commit
5cbf88670f
@ -606,4 +606,13 @@ RefPtr<T> MakeRefPtr(Args&&... aArgs) {
|
||||
|
||||
} // namespace mozilla
|
||||
|
||||
/**
|
||||
* Deduction guide to allow simple `RefPtr` definitions from an
|
||||
* already_AddRefed<T> without repeating the type, e.g.:
|
||||
*
|
||||
* RefPtr ptr = MakeAndAddRef<SomeType>(...);
|
||||
*/
|
||||
template <typename T>
|
||||
RefPtr(already_AddRefed<T>) -> RefPtr<T>;
|
||||
|
||||
#endif /* mozilla_RefPtr_h */
|
||||
|
@ -7,6 +7,8 @@
|
||||
#include "mozilla/RefPtr.h"
|
||||
#include "mozilla/RefCounted.h"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
using mozilla::RefCounted;
|
||||
|
||||
class Foo : public RefCounted<Foo> {
|
||||
@ -59,8 +61,10 @@ int main() {
|
||||
MOZ_RELEASE_ASSERT(1 == Foo::sNumDestroyed);
|
||||
|
||||
{
|
||||
RefPtr<Foo> f1 = NewFoo();
|
||||
RefPtr<Foo> f2(NewFoo());
|
||||
RefPtr f1 = NewFoo();
|
||||
static_assert(std::is_same_v<decltype(f1), RefPtr<Foo>>);
|
||||
RefPtr f2(NewFoo());
|
||||
static_assert(std::is_same_v<decltype(f2), RefPtr<Foo>>);
|
||||
MOZ_RELEASE_ASSERT(1 == Foo::sNumDestroyed);
|
||||
}
|
||||
MOZ_RELEASE_ASSERT(3 == Foo::sNumDestroyed);
|
||||
@ -109,7 +113,8 @@ int main() {
|
||||
MOZ_RELEASE_ASSERT(11 == Foo::sNumDestroyed);
|
||||
|
||||
{
|
||||
RefPtr<Foo> f = GetNullFoo();
|
||||
RefPtr f = GetNullFoo();
|
||||
static_assert(std::is_same_v<decltype(f), RefPtr<Foo>>);
|
||||
MOZ_RELEASE_ASSERT(11 == Foo::sNumDestroyed);
|
||||
}
|
||||
MOZ_RELEASE_ASSERT(11 == Foo::sNumDestroyed);
|
||||
|
Loading…
Reference in New Issue
Block a user