1 /*
2  * Copyright (c) 2012-2014 Glen Joseph Fernandes
3  * glenfe at live dot com
4  *
5  * Distributed under the Boost Software License,
6  * Version 1.0. (See accompanying file LICENSE_1_0.txt
7  * or copy at http://boost.org/LICENSE_1_0.txt)
8  */
9 #include <boost/detail/lightweight_test.hpp>
10 #include <boost/smart_ptr/allocate_shared_array.hpp>
11 #include <boost/smart_ptr/enable_shared_from_this.hpp>
12 
13 class type
14     : public boost::enable_shared_from_this<type> {
15 public:
16     static unsigned int instances;
17 
type()18     explicit type() {
19         instances++;
20     }
21 
~type()22     ~type() {
23         instances--;
24     }
25 
26 private:
27     type(const type&);
28     type& operator=(const type&);
29 };
30 
31 unsigned int type::instances = 0;
32 
main()33 int main() {
34     BOOST_TEST(type::instances == 0);
35     {
36         boost::shared_ptr<type[]> a1 = boost::allocate_shared<type[]>(std::allocator<type>(), 3);
37         try {
38             a1[0].shared_from_this();
39             BOOST_ERROR("shared_from_this did not throw");
40         } catch (...) {
41             BOOST_TEST(type::instances == 3);
42         }
43     }
44 
45     BOOST_TEST(type::instances == 0);
46     {
47         boost::shared_ptr<type[]> a1 = boost::allocate_shared_noinit<type[]>(std::allocator<type>(), 3);
48         try {
49             a1[0].shared_from_this();
50             BOOST_ERROR("shared_from_this did not throw");
51         } catch (...) {
52             BOOST_TEST(type::instances == 3);
53         }
54     }
55 
56     return boost::report_errors();
57 }
58