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 if (instances == 0) {
59 throw true;
60 }
61 ++instances;
62 }
63
64 ~type() {
65 --instances;
66 }
67
68private:
69 type(const type&);
70 type& operator=(const type&);
71};
72
73unsigned type::instances = 0;
74
75int main()
76{
77 try {
78 boost::allocate_unique<type>(alloc: creator<type>());
79 BOOST_ERROR("allocate_unique did not throw");
80 } catch (...) {
81 BOOST_TEST(type::instances == 0);
82 }
83 try {
84 boost::allocate_unique<const type>(alloc: creator<>());
85 BOOST_ERROR("allocate_unique did not throw");
86 } catch (...) {
87 BOOST_TEST(type::instances == 0);
88 }
89 return boost::report_errors();
90}
91#else
92int main()
93{
94 return 0;
95}
96#endif
97

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