[libcxx-commits] [libcxx] 8356405 - [libcxx] Add deduction guides for shared_ptr and weak_ptr

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Thu May 7 09:08:10 PDT 2020


Author: Logan Smith
Date: 2020-05-07T12:07:48-04:00
New Revision: 83564056d4b186c9fcf016cdbb388755009f7b5a

URL: https://github.com/llvm/llvm-project/commit/83564056d4b186c9fcf016cdbb388755009f7b5a
DIFF: https://github.com/llvm/llvm-project/commit/83564056d4b186c9fcf016cdbb388755009f7b5a.diff

LOG: [libcxx] Add deduction guides for shared_ptr and weak_ptr

This patch adds deduction guides to <memory> to allow deducing
construction of shared_ptrs from unique_ptrs, and from weak_ptrs
and vice versa, as specified by C++17.

Differential Revision: https://reviews.llvm.org/D69603

Added: 
    libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/deduction.pass.cpp
    libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.const/shared_ptr_deduction.pass.cpp

Modified: 
    libcxx/include/memory

Removed: 
    


################################################################################
diff  --git a/libcxx/include/memory b/libcxx/include/memory
index 729b8b5c874e..29c5e98b3b7c 100644
--- a/libcxx/include/memory
+++ b/libcxx/include/memory
@@ -450,6 +450,11 @@ public:
     template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
 };
 
+template<class T>
+shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
+template<class T, class D>
+shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
+
 // shared_ptr comparisons:
 template<class T, class U>
     bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
@@ -548,6 +553,9 @@ public:
     template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
 };
 
+template<class T>
+weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
+
 // weak_ptr specialized algorithms:
 template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
 
@@ -3965,6 +3973,12 @@ private:
     template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
 };
 
+#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
+template<class _Tp>
+shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
+template<class _Tp, class _Dp>
+shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
+#endif
 
 template<class _Tp>
 inline
@@ -4765,6 +4779,11 @@ public:
     template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
 };
 
+#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
+template<class _Tp>
+weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
+#endif
+
 template<class _Tp>
 inline
 _LIBCPP_CONSTEXPR

diff  --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/deduction.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/deduction.pass.cpp
new file mode 100644
index 000000000000..888cec620d7a
--- /dev/null
+++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/deduction.pass.cpp
@@ -0,0 +1,58 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <memory>
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+// UNSUPPORTED: libcpp-no-deduction-guides
+
+// template<class T> class shared_ptr
+
+// shared_ptr(weak_ptr<T>) -> shared_ptr<T>
+// shared_ptr(unique_ptr<T>) -> shared_ptr<T>
+
+#include <memory>
+#include <cassert>
+
+#include "test_macros.h"
+
+struct A {};
+
+struct D {
+  void operator()(void*) const {}
+};
+
+int main(int, char**)
+{
+  {
+    std::shared_ptr<A> s0(new A);
+    std::weak_ptr<A> w = s0;
+    auto s = std::shared_ptr(w);
+    ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>);
+    assert(s0.use_count() == 2);
+    assert(s.use_count() == 2);
+    assert(s0.get() == s.get());
+  }
+  {
+    std::unique_ptr<A> u(new A);
+    A* const uPointee = u.get();
+    std::shared_ptr s = std::move(u);
+    ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>);
+    assert(u == nullptr);
+    assert(s.get() == uPointee);
+  }
+  {
+    std::unique_ptr<A, D> u(new A, D{});
+    A* const uPointee = u.get();
+    std::shared_ptr s(std::move(u));
+    ASSERT_SAME_TYPE(decltype(s), std::shared_ptr<A>);
+    assert(u == nullptr);
+    assert(s.get() == uPointee);
+  }
+
+  return 0;
+}

diff  --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.const/shared_ptr_deduction.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.const/shared_ptr_deduction.pass.cpp
new file mode 100644
index 000000000000..9125ca5bd132
--- /dev/null
+++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.const/shared_ptr_deduction.pass.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <memory>
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+// UNSUPPORTED: libcpp-no-deduction-guides
+
+// template<class T> class weak_ptr
+
+// weak_ptr(shared_ptr<T>) -> weak_ptr<T>
+
+#include <memory>
+#include <cassert>
+
+#include "test_macros.h"
+
+struct A {};
+
+int main(int, char**)
+{
+  std::shared_ptr<A> s(new A);
+  auto w = std::weak_ptr(s);
+  ASSERT_SAME_TYPE(decltype(w), std::weak_ptr<A>);
+  assert(!w.expired());
+  assert(w.use_count() == 1);
+  assert(w.lock().get() == s.get());
+
+  return 0;
+}


        


More information about the libcxx-commits mailing list