Commit cfb1a244 authored by Bartek Nowierski's avatar Bartek Nowierski Committed by Commit Bot

Move swap() under base namespace

This is needed because of how argument-dependent lookup works (it
looks in the namespace of the type for an unqualified function name).

According to go/using-std-swap, the recommended usage pattern is:
  using std::swap;
  swap(thing1, thing2);
Having swap outside of base namespace wouldn't work with this
recommendation as slower std::swap would be silently used instead.

Bug: 1073933
Change-Id: Ib4485b51ba52ad491306bec9b4402a154adae16c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2167796
Auto-Submit: Bartek Nowierski <bartekn@chromium.org>
Commit-Queue: Łukasz Anforowicz <lukasza@chromium.org>
Reviewed-by: default avatarŁukasz Anforowicz <lukasza@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#763350}
parent 092c1026
...@@ -80,13 +80,13 @@ class CheckedPtr { ...@@ -80,13 +80,13 @@ class CheckedPtr {
T* ptr_ = nullptr; T* ptr_ = nullptr;
}; };
} // namespace base
using base::CheckedPtr;
template <typename T> template <typename T>
void swap(CheckedPtr<T>& lhs, CheckedPtr<T>& rhs) noexcept { void swap(CheckedPtr<T>& lhs, CheckedPtr<T>& rhs) noexcept {
lhs.swap(rhs); lhs.swap(rhs);
} }
} // namespace base
using base::CheckedPtr;
#endif // BASE_MEMORY_CHECKED_PTR_H_ #endif // BASE_MEMORY_CHECKED_PTR_H_
...@@ -11,8 +11,6 @@ ...@@ -11,8 +11,6 @@
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace base {
// This helps when copying arrays/vectors of pointers. // This helps when copying arrays/vectors of pointers.
static_assert(std::is_trivially_copyable<CheckedPtr<void>>::value, static_assert(std::is_trivially_copyable<CheckedPtr<void>>::value,
"CheckedPtr should be trivially copyable"); "CheckedPtr should be trivially copyable");
...@@ -33,8 +31,6 @@ struct Derived : MyStruct { ...@@ -33,8 +31,6 @@ struct Derived : MyStruct {
int y; int y;
}; };
} // namespace
TEST(CheckedPtr, NullStarDereference) { TEST(CheckedPtr, NullStarDereference) {
CheckedPtr<int> ptr; CheckedPtr<int> ptr;
EXPECT_DEATH_IF_SUPPORTED(if (*ptr == 42) return, ""); EXPECT_DEATH_IF_SUPPORTED(if (*ptr == 42) return, "");
...@@ -183,4 +179,4 @@ TEST(CheckedPtr, AdvanceString) { ...@@ -183,4 +179,4 @@ TEST(CheckedPtr, AdvanceString) {
} }
} }
} // namespace base } // namespace
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include "base/memory/checked_ptr.h" #include "base/memory/checked_ptr.h"
namespace base { namespace {
struct Producer {}; struct Producer {};
struct DerivedProducer : Producer {}; struct DerivedProducer : Producer {};
...@@ -17,7 +17,7 @@ struct OtherDerivedProducer : Producer {}; ...@@ -17,7 +17,7 @@ struct OtherDerivedProducer : Producer {};
struct Unrelated {}; struct Unrelated {};
struct DerivedUnrelated : Unrelated {}; struct DerivedUnrelated : Unrelated {};
#if defined(NCTEST_AUTO_DOWNCAST) // [r"no viable conversion from 'CheckedPtr<base::Producer>' to 'CheckedPtr<base::DerivedProducer>'"] #if defined(NCTEST_AUTO_DOWNCAST) // [r"no viable conversion from 'CheckedPtr<\(anonymous namespace\)::Producer>' to 'CheckedPtr<\(anonymous namespace\)::DerivedProducer>'"]
void WontCompile() { void WontCompile() {
Producer f; Producer f;
...@@ -25,7 +25,7 @@ void WontCompile() { ...@@ -25,7 +25,7 @@ void WontCompile() {
CheckedPtr<DerivedProducer> derived_ptr = ptr; CheckedPtr<DerivedProducer> derived_ptr = ptr;
} }
#elif defined(NCTEST_STATIC_DOWNCAST) // [r"no matching conversion for static_cast from 'CheckedPtr<base::Producer>' to 'CheckedPtr<base::DerivedProducer>'"] #elif defined(NCTEST_STATIC_DOWNCAST) // [r"no matching conversion for static_cast from 'CheckedPtr<\(anonymous namespace\)::Producer>' to 'CheckedPtr<\(anonymous namespace\)::DerivedProducer>'"]
void WontCompile() { void WontCompile() {
Producer f; Producer f;
...@@ -34,7 +34,7 @@ void WontCompile() { ...@@ -34,7 +34,7 @@ void WontCompile() {
static_cast<CheckedPtr<DerivedProducer>>(ptr); static_cast<CheckedPtr<DerivedProducer>>(ptr);
} }
#elif defined(NCTEST_AUTO_REF_DOWNCAST) // [r"non-const lvalue reference to type 'CheckedPtr<base::DerivedProducer>' cannot bind to a value of unrelated type 'CheckedPtr<base::Producer>'"] #elif defined(NCTEST_AUTO_REF_DOWNCAST) // [r"non-const lvalue reference to type 'CheckedPtr<\(anonymous namespace\)::DerivedProducer>' cannot bind to a value of unrelated type 'CheckedPtr<\(anonymous namespace\)::Producer>'"]
void WontCompile() { void WontCompile() {
Producer f; Producer f;
...@@ -42,7 +42,7 @@ void WontCompile() { ...@@ -42,7 +42,7 @@ void WontCompile() {
CheckedPtr<DerivedProducer>& derived_ptr = ptr; CheckedPtr<DerivedProducer>& derived_ptr = ptr;
} }
#elif defined(NCTEST_STATIC_REF_DOWNCAST) // [r"non-const lvalue reference to type 'CheckedPtr<base::DerivedProducer>' cannot bind to a value of unrelated type 'CheckedPtr<base::Producer>'"] #elif defined(NCTEST_STATIC_REF_DOWNCAST) // [r"non-const lvalue reference to type 'CheckedPtr<\(anonymous namespace\)::DerivedProducer>' cannot bind to a value of unrelated type 'CheckedPtr<\(anonymous namespace\)::Producer>'"]
void WontCompile() { void WontCompile() {
Producer f; Producer f;
...@@ -51,21 +51,21 @@ void WontCompile() { ...@@ -51,21 +51,21 @@ void WontCompile() {
static_cast<CheckedPtr<DerivedProducer>&>(ptr); static_cast<CheckedPtr<DerivedProducer>&>(ptr);
} }
#elif defined(NCTEST_AUTO_DOWNCAST_FROM_RAW) // [r"no viable conversion from 'base::Producer \*' to 'CheckedPtr<base::DerivedProducer>'"] #elif defined(NCTEST_AUTO_DOWNCAST_FROM_RAW) // [r"no viable conversion from '\(anonymous namespace\)::Producer \*' to 'CheckedPtr<\(anonymous namespace\)::DerivedProducer>'"]
void WontCompile() { void WontCompile() {
Producer f; Producer f;
CheckedPtr<DerivedProducer> ptr = &f; CheckedPtr<DerivedProducer> ptr = &f;
} }
#elif defined(NCTEST_UNRELATED_FROM_RAW) // [r"no viable conversion from 'base::DerivedProducer \*' to 'CheckedPtr<base::Unrelated>'"] #elif defined(NCTEST_UNRELATED_FROM_RAW) // [r"no viable conversion from '\(anonymous namespace\)::DerivedProducer \*' to 'CheckedPtr<\(anonymous namespace\)::Unrelated>'"]
void WontCompile() { void WontCompile() {
DerivedProducer f; DerivedProducer f;
CheckedPtr<Unrelated> ptr = &f; CheckedPtr<Unrelated> ptr = &f;
} }
#elif defined(NCTEST_UNRELATED_STATIC_FROM_WRAPPED) // [r"static_cast from 'base::DerivedProducer \*' to 'base::Unrelated \*', which are not related by inheritance, is not allowed"] #elif defined(NCTEST_UNRELATED_STATIC_FROM_WRAPPED) // [r"static_cast from '\(anonymous namespace\)::DerivedProducer \*' to '\(anonymous namespace\)::Unrelated \*', which are not related by inheritance, is not allowed"]
void WontCompile() { void WontCompile() {
DerivedProducer f; DerivedProducer f;
...@@ -83,4 +83,4 @@ void WontCompile() { ...@@ -83,4 +83,4 @@ void WontCompile() {
#endif #endif
} // namespace base } // namespace
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment