bind_member_function_tests.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <cmath>
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <boost/noncopyable.hpp>
  10. #include <boost/phoenix/core.hpp>
  11. #include <boost/phoenix/operator.hpp>
  12. #include <boost/phoenix/bind.hpp>
  13. namespace test
  14. {
  15. struct x //: boost::noncopyable // test non-copyable (hold this by reference)
  16. {
  17. void
  18. test() const
  19. {
  20. std::cout << "Test binding member functions...\n";
  21. }
  22. };
  23. struct y //: boost::noncopyable // test non-copyable (hold this by reference)
  24. {
  25. int
  26. negate(int n) const
  27. {
  28. return -n;
  29. }
  30. };
  31. struct z //: boost::noncopyable // test non-copyable (hold this by reference)
  32. {
  33. int
  34. plus(int a, int b) const
  35. {
  36. return a + b;
  37. }
  38. };
  39. struct zz //: boost::noncopyable // test non-copyable (hold this by reference)
  40. {
  41. int
  42. plus3(int a, int b, int c) const
  43. {
  44. return a + b + c;
  45. }
  46. };
  47. }
  48. int
  49. main()
  50. {
  51. using boost::phoenix::bind;
  52. using boost::phoenix::ref;
  53. using boost::phoenix::arg_names::arg1;
  54. using boost::phoenix::arg_names::arg2;
  55. using boost::phoenix::arg_names::arg3;
  56. int a = 123;
  57. int b = 256;
  58. test::x x_;
  59. test::y y_;
  60. test::z z_;
  61. test::zz zz_;
  62. bind(&test::x::test, x_)();
  63. BOOST_TEST(bind(&test::y::negate, y_, arg1)(a) == -a);
  64. BOOST_TEST(bind(&test::z::plus, arg1, arg2, arg3)(z_, a, b) == a+b);
  65. BOOST_TEST(bind(&test::zz::plus3, zz_, arg1, arg2, arg3)(a, b, a) == a+b+a);
  66. BOOST_TEST(bind(&test::y::negate, &y_, 777)(a) == -777);
  67. return boost::report_errors();
  68. }