factory_default_allocator.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. Copyright 2019 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/functional/factory.hpp>
  8. #include <boost/core/default_allocator.hpp>
  9. #include <boost/core/lightweight_test.hpp>
  10. #include <boost/smart_ptr/shared_ptr.hpp>
  11. #include <boost/config.hpp>
  12. #include <exception>
  13. #if defined(BOOST_NO_EXCEPTIONS)
  14. namespace boost {
  15. BOOST_NORETURN void throw_exception(const std::exception&)
  16. {
  17. std::terminate();
  18. }
  19. }
  20. #endif
  21. class sum {
  22. public:
  23. sum(int a, int b)
  24. : value_(a + b) { }
  25. int get() const {
  26. return value_;
  27. }
  28. private:
  29. int value_;
  30. };
  31. int main()
  32. {
  33. int a = 1;
  34. int b = 2;
  35. {
  36. boost::shared_ptr<sum> s(boost::factory<boost::shared_ptr<sum>,
  37. boost::default_allocator<void>,
  38. boost::factory_alloc_for_pointee_and_deleter>()(a, b));
  39. BOOST_TEST(s->get() == 3);
  40. }
  41. {
  42. boost::shared_ptr<sum> s(boost::factory<boost::shared_ptr<sum>,
  43. boost::default_allocator<void>,
  44. boost::factory_passes_alloc_to_smart_pointer>()(a, b));
  45. BOOST_TEST(s->get() == 3);
  46. }
  47. return boost::report_errors();
  48. }