-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_unique_for_overwrite.h
73 lines (60 loc) · 2.41 KB
/
make_unique_for_overwrite.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef COOL_MAKE_UNIQUE_FOR_OVERWRITE_H_
#define COOL_MAKE_UNIQUE_FOR_OVERWRITE_H_
///////////////////////////////////////////////////////////////////////////////
// make_unique_for_overwrite<T>
//
// make_unique_for_overwrite is a replacement for make_unique which
// default initializes (as opposed to value initializes) both single objects
// (when no args are provided) and arrays of unknown bounds.
//
// std::unique_ptr<T> make_unique_for_overwrite<T>()
// default-initialized T
//
// std::unique_ptr<T> make_unique_for_overwrite<T>(Arg&& arg, Args&&... args)
// same as make_unique<T>(Arg&&, Args&&...)
//
// std::unique_ptr<T[]> make_unique_for_overwrite<T[]>(size_t n)
// default-initialized array of T of size n
//
// make_unique_for_overwrite<T[n]>(Args&&...)
// deleted (same as make_unique<T[n]>(Args&&...))
//
///////////////////////////////////////////////////////////////////////////////
#include <memory>
#include <type_traits>
namespace cool
{
namespace detail
{
// make_unique_if are helpers for make_unique_for_overwrite
template<typename T>
struct make_unique_if
{ using single_object = std::unique_ptr<T>; };
template<typename T>
struct make_unique_if<T[]>
{ using array = std::unique_ptr<T[]>; };
template<typename T, size_t N>
struct make_unique_if<T[N]>
{ using bound = void; };
} // detail namespace
// std::unique_ptr<T> make_unique_for_overwrite<T>()
template<typename T>
typename detail::make_unique_if<T>::single_object
make_unique_for_overwrite()
{ return std::unique_ptr<T>(new T); }
// std::unique_ptr<T> make_unique_for_overwrite<T>(Arg&& arg, Args&&... args)
template<typename T, typename... Args>
typename detail::make_unique_if<T>::single_object
make_unique_for_overwrite(Args&&... args)
{ return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); }
// std::unique_ptr<T[]> make_unique_for_overwrite<T[]>(size_t n)
template<typename T>
typename detail::make_unique_if<T>::array
make_unique_for_overwrite(size_t n)
{ return std::unique_ptr<T>(new std::remove_extent_t<T>[n]); }
// make_unique_for_overwrite<T[n]>(Args&&...)
template<typename T, typename... Args>
typename detail::make_unique_if<T>::bound
make_unique_for_overwrite(Args&&...) = delete;
} // cool namespace
#endif /* COOL_MAKE_UNIQUE_FOR_OVERWRITE_H_ */