factory_with_std_allocator.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. Copyright 2007 Tobias Schwinger
  3. Copyright 2017 Daniel James
  4. Copyright 2019 Glen Joseph Fernandes
  5. (glenjofe@gmail.com)
  6. Distributed under the Boost Software License, Version 1.0.
  7. (http://www.boost.org/LICENSE_1_0.txt)
  8. */
  9. #include <boost/functional/factory.hpp>
  10. #include <boost/core/lightweight_test.hpp>
  11. #include <boost/smart_ptr/shared_ptr.hpp>
  12. #include <memory>
  13. class sum {
  14. public:
  15. sum(int a, int b)
  16. : value_(a + b) { }
  17. int get() const {
  18. return value_;
  19. }
  20. private:
  21. int value_;
  22. };
  23. int main()
  24. {
  25. int a = 1;
  26. int b = 2;
  27. {
  28. boost::shared_ptr<sum> s(boost::factory<boost::shared_ptr<sum>,
  29. std::allocator<char>,
  30. boost::factory_alloc_for_pointee_and_deleter>()(a, b));
  31. BOOST_TEST(s->get() == 3);
  32. }
  33. {
  34. boost::shared_ptr<sum> s(boost::factory<boost::shared_ptr<sum>,
  35. std::allocator<char>,
  36. boost::factory_passes_alloc_to_smart_pointer>()(a, b));
  37. BOOST_TEST(s->get() == 3);
  38. }
  39. return boost::report_errors();
  40. }