[libcxx] r185352 - Implement n3656 - make_unique. Thanks to Howard for the review and suggestions.
Marshall Clow
mclow at qualcomm.com
Mon Jul 1 11:16:03 PDT 2013
Author: marshall
Date: Mon Jul 1 13:16:03 2013
New Revision: 185352
URL: http://llvm.org/viewvc/llvm-project?rev=185352&view=rev
Log:
Implement n3656 - make_unique. Thanks to Howard for the review and suggestions.
Added:
libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/
libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array.pass.cpp
libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array1.fail.cpp
libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array2.fail.cpp
libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array3.fail.cpp
libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array4.fail.cpp
libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.single.pass.cpp
Modified:
libcxx/trunk/include/memory
Modified: libcxx/trunk/include/memory
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/memory?rev=185352&r1=185351&r2=185352&view=diff
==============================================================================
--- libcxx/trunk/include/memory (original)
+++ libcxx/trunk/include/memory Mon Jul 1 13:16:03 2013
@@ -350,6 +350,10 @@ class bad_weak_ptr
bad_weak_ptr() noexcept;
};
+template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
+template<class T> unique_ptr<T> make_unique(size_t n); // C++14
+template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
+
template<class T>
class shared_ptr
{
@@ -3079,6 +3083,47 @@ move(unique_ptr<_Tp, _Dp>& __t)
#endif
+#if _LIBCPP_STD_VER > 11
+
+template<class _Tp>
+struct __unique_if
+{
+ typedef unique_ptr<_Tp> __unique_single;
+};
+
+template<class _Tp>
+struct __unique_if<_Tp[]>
+{
+ typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
+};
+
+template<class _Tp, size_t _Np>
+struct __unique_if<_Tp[_Np]>
+{
+ typedef void __unique_array_known_bound;
+};
+
+template<class _Tp, class... _Args>
+typename __unique_if<_Tp>::__unique_single
+make_unique(_Args&&... __args)
+{
+ return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
+}
+
+template<class _Tp>
+typename __unique_if<_Tp>::__unique_array_unknown_bound
+make_unique(size_t __n)
+{
+ typedef typename remove_extent<_Tp>::type _Up;
+ return unique_ptr<_Tp>(new _Up[__n]());
+}
+
+template<class _Tp, class... _Args>
+ typename __unique_if<_Tp>::__unique_array_known_bound
+ make_unique(_Args&&...) = delete;
+
+#endif // _LIBCPP_STD_VER > 11
+
template <class _Tp> struct hash;
// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
Added: libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array.pass.cpp?rev=185352&view=auto
==============================================================================
--- libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array.pass.cpp (added)
+++ libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array.pass.cpp Mon Jul 1 13:16:03 2013
@@ -0,0 +1,45 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include <memory>
+#include <string>
+#include <cassert>
+
+// The only way to create an unique_ptr<T[]> is to default construct them.
+
+class foo {
+public:
+ foo () : val_(3) {}
+ int get () const { return val_; }
+private:
+ int val_;
+ };
+
+int main()
+{
+#if _LIBCPP_STD_VER > 11
+ {
+ auto p1 = std::make_unique<int[]>(5);
+ for ( int i = 0; i < 5; ++i )
+ assert ( p1[i] == 0 );
+ }
+
+ {
+ auto p2 = std::make_unique<std::string[]>(5);
+ for ( int i = 0; i < 5; ++i )
+ assert ( p2[i].size () == 0 );
+ }
+
+ {
+ auto p3 = std::make_unique<foo[]>(7);
+ for ( int i = 0; i < 7; ++i )
+ assert ( p3[i].get () == 3 );
+ }
+#endif // _LIBCPP_STD_VER > 11
+}
Added: libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array1.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array1.fail.cpp?rev=185352&view=auto
==============================================================================
--- libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array1.fail.cpp (added)
+++ libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array1.fail.cpp Mon Jul 1 13:16:03 2013
@@ -0,0 +1,17 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include <memory>
+#include <string>
+#include <cassert>
+
+int main()
+{
+ auto up1 = std::make_unique<std::string[]>("error"); // doesn't compile - no bound
+}
Added: libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array2.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array2.fail.cpp?rev=185352&view=auto
==============================================================================
--- libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array2.fail.cpp (added)
+++ libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array2.fail.cpp Mon Jul 1 13:16:03 2013
@@ -0,0 +1,17 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include <memory>
+#include <string>
+#include <cassert>
+
+int main()
+{
+ auto up2 = std::make_unique<int[]>(10, 20, 30, 40);
+}
Added: libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array3.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array3.fail.cpp?rev=185352&view=auto
==============================================================================
--- libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array3.fail.cpp (added)
+++ libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array3.fail.cpp Mon Jul 1 13:16:03 2013
@@ -0,0 +1,17 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include <memory>
+#include <string>
+#include <cassert>
+
+int main()
+{
+ auto up3 = std::make_unique<int[5]>(); // this is deleted
+}
Added: libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array4.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array4.fail.cpp?rev=185352&view=auto
==============================================================================
--- libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array4.fail.cpp (added)
+++ libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array4.fail.cpp Mon Jul 1 13:16:03 2013
@@ -0,0 +1,17 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include <memory>
+#include <string>
+#include <cassert>
+
+int main()
+{
+ auto up4 = std::make_unique<int[5]>(11, 22, 33, 44, 55); // deleted
+}
Added: libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.single.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.single.pass.cpp?rev=185352&view=auto
==============================================================================
--- libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.single.pass.cpp (added)
+++ libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.single.pass.cpp Mon Jul 1 13:16:03 2013
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include <memory>
+#include <string>
+#include <cassert>
+
+int main()
+{
+#if _LIBCPP_STD_VER > 11
+ {
+ std::unique_ptr<int> p1 = std::make_unique<int>(1);
+ assert ( *p1 == 1 );
+ p1 = std::make_unique<int> ();
+ assert ( *p1 == 0 );
+ }
+
+ {
+ std::unique_ptr<std::string> p2 = std::make_unique<std::string> ( "Meow!" );
+ assert ( *p2 == "Meow!" );
+ p2 = std::make_unique<std::string> ();
+ assert ( *p2 == "" );
+ p2 = std::make_unique<std::string> ( 6, 'z' );
+ assert ( *p2 == "zzzzzz" );
+ }
+#endif // _LIBCPP_STD_VER > 11
+}
More information about the cfe-commits
mailing list