make_shared_array_esft_test.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. Copyright 2012-2015 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <boost/core/lightweight_test.hpp>
  8. #include <boost/smart_ptr/enable_shared_from_this.hpp>
  9. #include <boost/smart_ptr/make_shared.hpp>
  10. class type
  11. : public boost::enable_shared_from_this<type> {
  12. public:
  13. static unsigned instances;
  14. type() {
  15. ++instances;
  16. }
  17. ~type() {
  18. --instances;
  19. }
  20. private:
  21. type(const type&);
  22. type& operator=(const type&);
  23. };
  24. unsigned type::instances = 0;
  25. int main()
  26. {
  27. BOOST_TEST(type::instances == 0);
  28. {
  29. boost::shared_ptr<type[]> result =
  30. boost::make_shared<type[]>(3);
  31. try {
  32. result[0].shared_from_this();
  33. BOOST_ERROR("shared_from_this did not throw");
  34. } catch (...) {
  35. BOOST_TEST(type::instances == 3);
  36. }
  37. }
  38. BOOST_TEST(type::instances == 0);
  39. {
  40. boost::shared_ptr<type[3]> result =
  41. boost::make_shared_noinit<type[3]>();
  42. try {
  43. result[0].shared_from_this();
  44. BOOST_ERROR("shared_from_this did not throw");
  45. } catch (...) {
  46. BOOST_TEST(type::instances == 3);
  47. }
  48. }
  49. return boost::report_errors();
  50. }