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 !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
13#include <boost/smart_ptr/allocate_unique.hpp>
14#include <boost/core/lightweight_test.hpp>
15
16template<class T = void>
17struct 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
40template<class T, class U>
41inline bool
42operator==(const creator<T>&, const creator<U>&)
43{
44 return true;
45}
46
47template<class T, class U>
48inline bool
49operator!=(const creator<T>&, const creator<U>&)
50{
51 return false;
52}
53
54struct type {
55 int x;
56 int y;
57};
58
59int main()
60{
61 {
62 std::unique_ptr<type,
63 boost::alloc_deleter<type, creator<type> > > result =
64 boost::allocate_unique<type>(alloc: creator<type>(), value: { .x: 1, .y: 2 });
65 BOOST_TEST(result.get() != 0);
66 BOOST_TEST(result->x == 1);
67 BOOST_TEST(result->y == 2);
68 }
69 {
70 std::unique_ptr<const type,
71 boost::alloc_deleter<const type, creator<> > > result =
72 boost::allocate_unique<const type>(alloc: creator<>(), value: { .x: 1, .y: 2 });
73 BOOST_TEST(result.get() != 0);
74 BOOST_TEST(result->x == 1);
75 BOOST_TEST(result->y == 2);
76 }
77 return boost::report_errors();
78}
79#else
80int main()
81{
82 return 0;
83}
84#endif
85

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