diff options
Diffstat (limited to 'absl/memory')
-rw-r--r-- | absl/memory/memory.h | 25 | ||||
-rw-r--r-- | absl/memory/memory_test.cc | 10 |
2 files changed, 26 insertions, 9 deletions
diff --git a/absl/memory/memory.h b/absl/memory/memory.h index c67996084ef6..15cd85f4e1ed 100644 --- a/absl/memory/memory.h +++ b/absl/memory/memory.h @@ -38,8 +38,8 @@ namespace absl { // Function Template: WrapUnique() // ----------------------------------------------------------------------------- // -// Transfers ownership of a raw pointer to a `std::unique_ptr`. The returned -// value is a `std::unique_ptr` of deduced type. +// Adopts ownership from a raw pointer and transfers it to the returned +// `std::unique_ptr`, whose type is deduced. // // Example: // X* NewX(int, int); @@ -81,6 +81,9 @@ struct MakeUniqueResult<T[N]> { } // namespace memory_internal +#if __cplusplus >= 201402L || defined(_MSC_VER) +using std::make_unique; +#else // ----------------------------------------------------------------------------- // Function Template: make_unique<T>() // ----------------------------------------------------------------------------- @@ -164,13 +167,14 @@ typename memory_internal::MakeUniqueResult<T>::array make_unique(size_t n) { template <typename T, typename... Args> typename memory_internal::MakeUniqueResult<T>::invalid make_unique( Args&&... /* args */) = delete; +#endif // ----------------------------------------------------------------------------- // Function Template: RawPtr() // ----------------------------------------------------------------------------- // -// Extracts the raw pointer from a pointer-like 'ptr'. `absl::RawPtr` is useful -// within templates that need to handle a complement of raw pointers, +// Extracts the raw pointer from a pointer-like value `ptr`. `absl::RawPtr` is +// useful within templates that need to handle a complement of raw pointers, // `std::nullptr_t`, and smart pointers. template <typename T> auto RawPtr(T&& ptr) -> decltype(&*ptr) { @@ -183,9 +187,9 @@ inline std::nullptr_t RawPtr(std::nullptr_t) { return nullptr; } // Function Template: ShareUniquePtr() // ----------------------------------------------------------------------------- // -// Transforms a `std::unique_ptr` rvalue into a `std::shared_ptr`. The returned -// value is a `std::shared_ptr` of deduced type and ownership is transferred to -// the shared pointer. +// Adopts a `std::unique_ptr` rvalue and returns a `std::shared_ptr` of deduced +// type. Ownership (if any) of the held value is transferred to the returned +// shared pointer. // // Example: // @@ -194,8 +198,11 @@ inline std::nullptr_t RawPtr(std::nullptr_t) { return nullptr; } // CHECK_EQ(*sp, 10); // CHECK(up == nullptr); // -// Note that this conversion is correct even when T is an array type, although -// the resulting shared pointer may not be very useful. +// Note that this conversion is correct even when T is an array type, and more +// generally it works for *any* deleter of the `unique_ptr` (single-object +// deleter, array deleter, or any custom deleter), since the deleter is adopted +// by the shared pointer as well. The deleter is copied (unless it is a +// reference). // // Implements the resolution of [LWG 2415](http://wg21.link/lwg2415), by which a // null shared pointer does not attempt to call the deleter. diff --git a/absl/memory/memory_test.cc b/absl/memory/memory_test.cc index 8a5f5522a089..7d047ca0c726 100644 --- a/absl/memory/memory_test.cc +++ b/absl/memory/memory_test.cc @@ -138,6 +138,16 @@ TEST(Make_UniqueTest, Array) { EXPECT_THAT(ArrayWatch::allocs(), ElementsAre(5 * sizeof(ArrayWatch))); } +TEST(Make_UniqueTest, NotAmbiguousWithStdMakeUnique) { + // Ensure that absl::make_unique is not ambiguous with std::make_unique. + // In C++14 mode, the below call to make_unique has both types as candidates. + struct TakesStdType { + explicit TakesStdType(const std::vector<int> &vec) {} + }; + using absl::make_unique; + make_unique<TakesStdType>(std::vector<int>()); +} + #if 0 // TODO(billydonahue): Make a proper NC test. // These tests shouldn't compile. |