1/*
2Copyright 2019 Glen Joseph Fernandes
3(glenjofe@gmail.com)
4
5Distributed under the Boost Software License, Version 1.0.
6(http://www.boost.org/LICENSE_1_0.txt)
7*/
8#include <boost/config.hpp>
9#if (!defined(BOOST_LIBSTDCXX_VERSION) || \
10 BOOST_LIBSTDCXX_VERSION >= 46000) && \
11 !defined(BOOST_NO_CXX11_SMART_PTR)
12#include <boost/smart_ptr/allocate_unique.hpp>
13#include <boost/core/lightweight_test.hpp>
14
15template<class T = void>
16struct creator {
17 typedef T value_type;
18 typedef T* pointer;
19
20 template<class U>
21 struct rebind {
22 typedef creator<U> other;
23 };
24
25 creator() { }
26
27 template<class U>
28 creator(const creator<U>&) { }
29
30 T* allocate(std::size_t size) {
31 return static_cast<T*>(::operator new(sizeof(T) * size));
32 }
33
34 void deallocate(T* ptr, std::size_t) {
35 ::operator delete(ptr);
36 }
37};
38
39template<class T, class U>
40inline bool
41operator==(const creator<T>&, const creator<U>&)
42{
43 return true;
44}
45
46template<class T, class U>
47inline bool
48operator!=(const creator<T>&, const creator<U>&)
49{
50 return false;
51}
52
53class type {
54public:
55 static unsigned instances;
56
57 type()
58 : value_(0.0) {
59 ++instances;
60 }
61
62 ~type() {
63 --instances;
64 }
65
66 void set(long double value) {
67 value_ = value;
68 }
69
70 long double get() const {
71 return value_;
72 }
73
74private:
75 type(const type&);
76 type& operator=(const type&);
77
78 long double value_;
79};
80
81unsigned type::instances = 0;
82
83int main()
84{
85 {
86 std::unique_ptr<int,
87 boost::alloc_deleter<int,
88 boost::noinit_adaptor<creator<int> > > > result =
89 boost::allocate_unique_noinit<int>(alloc: creator<int>());
90 BOOST_TEST(result.get() != 0);
91 }
92 {
93 std::unique_ptr<const int,
94 boost::alloc_deleter<const int,
95 boost::noinit_adaptor<creator<> > > > result =
96 boost::allocate_unique_noinit<const int>(alloc: creator<>());
97 BOOST_TEST(result.get() != 0);
98 }
99 {
100 std::unique_ptr<type,
101 boost::alloc_deleter<type,
102 boost::noinit_adaptor<creator<type> > > > result =
103 boost::allocate_unique_noinit<type>(alloc: creator<type>());
104 BOOST_TEST(result.get() != 0);
105 BOOST_TEST(type::instances == 1);
106 result.reset();
107 BOOST_TEST(type::instances == 0);
108 }
109 {
110 std::unique_ptr<const type,
111 boost::alloc_deleter<const type,
112 boost::noinit_adaptor<creator<> > > > result =
113 boost::allocate_unique_noinit<const type>(alloc: creator<>());
114 BOOST_TEST(result.get() != 0);
115 BOOST_TEST(type::instances == 1);
116 result.reset();
117 BOOST_TEST(type::instances == 0);
118 }
119 return boost::report_errors();
120}
121#else
122int main()
123{
124 return 0;
125}
126#endif
127

source code of boost/libs/smart_ptr/test/allocate_unique_noinit_test.cpp