1 | /* |
2 | Copyright 2019 Glen Joseph Fernandes |
3 | (glenjofe@gmail.com) |
4 | |
5 | Distributed 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 >= 48000) && \ |
11 | !defined(BOOST_NO_CXX11_SMART_PTR) && \ |
12 | !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) |
13 | #include <boost/smart_ptr/allocate_unique.hpp> |
14 | #include <boost/core/lightweight_test.hpp> |
15 | |
16 | template<class T = void> |
17 | struct creator { |
18 | typedef T value_type; |
19 | typedef T* pointer; |
20 | |
21 | template<class U> |
22 | struct rebind { |
23 | typedef creator<U> other; |
24 | }; |
25 | |
26 | creator() { } |
27 | |
28 | template<class U> |
29 | creator(const creator<U>&) { } |
30 | |
31 | T* allocate(std::size_t size) { |
32 | return static_cast<T*>(::operator new(sizeof(T) * size)); |
33 | } |
34 | |
35 | void deallocate(T* ptr, std::size_t) { |
36 | ::operator delete(ptr); |
37 | } |
38 | }; |
39 | |
40 | template<class T, class U> |
41 | inline bool |
42 | operator==(const creator<T>&, const creator<U>&) |
43 | { |
44 | return true; |
45 | } |
46 | |
47 | template<class T, class U> |
48 | inline bool |
49 | operator!=(const creator<T>&, const creator<U>&) |
50 | { |
51 | return false; |
52 | } |
53 | |
54 | int main() |
55 | { |
56 | { |
57 | std::unique_ptr<int[][2], |
58 | boost::alloc_deleter<int[][2], creator<int> > > result = |
59 | boost::allocate_unique<int[][2]>(alloc: creator<int>(), size: 2, value: {0, 1}); |
60 | BOOST_TEST(result[0][0] == 0); |
61 | BOOST_TEST(result[0][1] == 1); |
62 | BOOST_TEST(result[1][0] == 0); |
63 | BOOST_TEST(result[1][1] == 1); |
64 | } |
65 | { |
66 | std::unique_ptr<int[][2], |
67 | boost::alloc_deleter<int[2][2], creator<int> > > result = |
68 | boost::allocate_unique<int[2][2]>(alloc: creator<int>(), value: {0, 1}); |
69 | BOOST_TEST(result[0][0] == 0); |
70 | BOOST_TEST(result[0][1] == 1); |
71 | BOOST_TEST(result[1][0] == 0); |
72 | BOOST_TEST(result[1][1] == 1); |
73 | } |
74 | { |
75 | std::unique_ptr<const int[][2], |
76 | boost::alloc_deleter<const int[][2], creator<> > > result = |
77 | boost::allocate_unique<const int[][2]>(alloc: creator<>(), size: 2, value: {0, 1}); |
78 | BOOST_TEST(result[0][0] == 0); |
79 | BOOST_TEST(result[0][1] == 1); |
80 | BOOST_TEST(result[1][0] == 0); |
81 | BOOST_TEST(result[1][1] == 1); |
82 | } |
83 | { |
84 | std::unique_ptr<const int[][2], |
85 | boost::alloc_deleter<const int[2][2], creator<> > > result = |
86 | boost::allocate_unique<const int[2][2]>(alloc: creator<>(), value: {0, 1}); |
87 | BOOST_TEST(result[0][0] == 0); |
88 | BOOST_TEST(result[0][1] == 1); |
89 | BOOST_TEST(result[1][0] == 0); |
90 | BOOST_TEST(result[1][1] == 1); |
91 | } |
92 | return boost::report_errors(); |
93 | } |
94 | #else |
95 | int main() |
96 | { |
97 | return 0; |
98 | } |
99 | #endif |
100 | |