adapt_function.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*=============================================================================
  2. Copyright (c) 2011 Thomas Heller
  3. Copyright (c) 2015 John Fletcher
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <iostream>
  8. #include <cmath>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <boost/phoenix/core.hpp>
  11. #include <boost/phoenix/function.hpp>
  12. namespace impl
  13. {
  14. void
  15. test()
  16. {
  17. std::cout << "Test adapting functions...\n";
  18. }
  19. int
  20. negate(int n)
  21. {
  22. return -n;
  23. }
  24. int
  25. plus(int a, int b)
  26. {
  27. return a + b;
  28. }
  29. template <typename T>
  30. T
  31. plus(T a, T b, T c)
  32. {
  33. return a + b + c;
  34. }
  35. int
  36. plus4(int a, int b, int c, int d)
  37. {
  38. return a + b + c + d;
  39. }
  40. }
  41. BOOST_PHOENIX_ADAPT_FUNCTION_NULLARY(void, test, impl::test)
  42. BOOST_PHOENIX_ADAPT_FUNCTION(int, negate, impl::negate, 1)
  43. BOOST_PHOENIX_ADAPT_FUNCTION(int, plus, impl::plus, 2)
  44. BOOST_PHOENIX_ADAPT_FUNCTION(
  45. typename boost::remove_reference<A0>::type
  46. , plus
  47. , impl::plus
  48. , 3
  49. )
  50. BOOST_PHOENIX_ADAPT_FUNCTION(int, plus4, impl::plus4, 4)
  51. // Test of solution to bug when using namespace
  52. using namespace boost::phoenix;
  53. BOOST_PHOENIX_ADAPT_FUNCTION_NULLARY(void, test2, impl::test)
  54. BOOST_PHOENIX_ADAPT_FUNCTION(int, negate2, impl::negate, 1)
  55. int
  56. main()
  57. {
  58. using boost::phoenix::arg_names::arg1;
  59. using boost::phoenix::arg_names::arg2;
  60. int a = 123;
  61. int b = 256;
  62. test()();
  63. test2()();
  64. BOOST_TEST(::negate(arg1)(a) == -a);
  65. BOOST_TEST(::negate2(arg1)(a) == -a);
  66. BOOST_TEST(::plus(arg1, arg2)(a, b) == a+b);
  67. BOOST_TEST(::plus(arg1, arg2, 3)(a, b) == a+b+3);
  68. BOOST_TEST(plus4(arg1, arg2, 3, 4)(a, b) == a+b+3+4);
  69. return boost::report_errors();
  70. }