1#include <boost/config.hpp>
2
3// shared_ptr_alloc11_test.cpp
4//
5// Test the allocator constructor with a C++11 minimal allocator
6//
7// Copyright (c) 2005, 2014 Peter Dimov
8//
9// Distributed under the Boost Software License, Version 1.0.
10// See accompanying file LICENSE_1_0.txt or copy at
11// http://www.boost.org/LICENSE_1_0.txt
12
13
14#include <boost/core/lightweight_test.hpp>
15#include <boost/shared_ptr.hpp>
16#include <memory>
17#include <cstddef>
18
19#if !defined( BOOST_NO_CXX11_ALLOCATOR )
20
21template< class T > class cxx11_allocator
22{
23public:
24
25 typedef T value_type;
26
27 cxx11_allocator()
28 {
29 }
30
31 template< class Y > cxx11_allocator( cxx11_allocator<Y> const & )
32 {
33 }
34
35 T * allocate( std::size_t n )
36 {
37 return static_cast< T* >( ::operator new( n * sizeof( T ) ) );
38 }
39
40 void deallocate( T * p, std::size_t n )
41 {
42 ::operator delete( p );
43 }
44};
45
46//
47
48struct D;
49
50struct X
51{
52 static int instances;
53
54 X(): deleted_( false )
55 {
56 ++instances;
57 }
58
59 ~X()
60 {
61 BOOST_TEST( deleted_ );
62 --instances;
63 }
64
65private:
66
67 friend struct D;
68
69 bool deleted_;
70
71 X( X const & );
72 X & operator=( X const & );
73};
74
75int X::instances = 0;
76
77struct D
78{
79 void operator()( X * px ) const
80 {
81 px->deleted_ = true;
82 delete px;
83 }
84};
85
86int main()
87{
88 BOOST_TEST( X::instances == 0 );
89
90 boost::shared_ptr<void> pv( new X, D(), cxx11_allocator<X>() );
91
92 BOOST_TEST( X::instances == 1 );
93
94 pv.reset();
95
96 BOOST_TEST( X::instances == 0 );
97
98 pv.reset( p: new X, d: D(), a: cxx11_allocator<void>() );
99
100 BOOST_TEST( X::instances == 1 );
101
102 pv.reset();
103
104 BOOST_TEST( X::instances == 0 );
105
106 return boost::report_errors();
107}
108
109#else // !defined( BOOST_NO_CXX11_ALLOCATOR )
110
111int main()
112{
113 return 0;
114}
115
116#endif
117

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