make_local_shared_array_esft_test.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. Copyright 2017 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/config.hpp>
  8. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
  9. !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  10. #include <boost/core/lightweight_test.hpp>
  11. #include <boost/smart_ptr/enable_shared_from_this.hpp>
  12. #include <boost/smart_ptr/make_local_shared.hpp>
  13. class type
  14. : public boost::enable_shared_from_this<type> {
  15. public:
  16. static unsigned instances;
  17. type() {
  18. ++instances;
  19. }
  20. ~type() {
  21. --instances;
  22. }
  23. private:
  24. type(const type&);
  25. type& operator=(const type&);
  26. };
  27. unsigned type::instances = 0;
  28. int main()
  29. {
  30. BOOST_TEST(type::instances == 0);
  31. {
  32. boost::local_shared_ptr<type[]> result =
  33. boost::make_local_shared<type[]>(3);
  34. try {
  35. result[0].shared_from_this();
  36. BOOST_ERROR("shared_from_this did not throw");
  37. } catch (...) {
  38. BOOST_TEST(type::instances == 3);
  39. }
  40. }
  41. BOOST_TEST(type::instances == 0);
  42. {
  43. boost::local_shared_ptr<type[3]> result =
  44. boost::make_local_shared_noinit<type[3]>();
  45. try {
  46. result[0].shared_from_this();
  47. BOOST_ERROR("shared_from_this did not throw");
  48. } catch (...) {
  49. BOOST_TEST(type::instances == 3);
  50. }
  51. }
  52. return boost::report_errors();
  53. }
  54. #else
  55. int main()
  56. {
  57. return 0;
  58. }
  59. #endif