new_delete_tests.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*=============================================================================
  2. Copyright (c) 2001-2007 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #include <iostream>
  7. #include <vector>
  8. #include <algorithm>
  9. #include <boost/shared_ptr.hpp>
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <boost/phoenix/core.hpp>
  12. #include <boost/phoenix/object.hpp>
  13. #include <boost/phoenix/operator.hpp>
  14. int n = 0;
  15. using std::cout;
  16. using std::endl;
  17. struct X
  18. {
  19. X(int, int, int) { cout << "new X(int, int, int)" << endl; ++n; }
  20. X() { cout << "new X" << endl; ++n; }
  21. ~X() { cout << "delete X" << endl; --n; }
  22. };
  23. int
  24. main()
  25. {
  26. using boost::phoenix::arg_names::arg1;
  27. using boost::phoenix::construct;
  28. using boost::phoenix::delete_;
  29. using boost::phoenix::new_;
  30. using std::for_each;
  31. using std::vector;
  32. {
  33. vector<X*> v(10);
  34. for_each(v.begin(), v.end(), arg1 = new_<X>());
  35. for_each(v.begin(), v.end(), delete_(arg1));
  36. for_each(v.begin(), v.end(), arg1 = new_<X>(1, 2, 3));
  37. for_each(v.begin(), v.end(), delete_(arg1));
  38. }
  39. {
  40. using boost::shared_ptr;
  41. vector<shared_ptr<X> > v(10);
  42. for_each(v.begin(), v.end(),
  43. arg1 = boost::phoenix::construct<shared_ptr<X> >(new_<X>())
  44. );
  45. }
  46. BOOST_TEST(n == 0);
  47. return boost::report_errors();
  48. }