Implement n3656 - make_unique. Thanks to Howard for the review and suggestions.

llvm-svn: 185352
This commit is contained in:
Marshall Clow 2013-07-01 18:16:03 +00:00
parent 7a9fcdf6fb
commit 28d8ba5f79
7 changed files with 191 additions and 0 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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
}

View File

@ -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);
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}