lambda.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*=============================================================================
  2. Copyright (c) 2017 Paul Fultz II
  3. lambda.cpp
  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 <boost/hof/lambda.hpp>
  8. #include <boost/hof/first_of.hpp>
  9. #include <boost/hof/partial.hpp>
  10. #include <boost/hof/infix.hpp>
  11. #include <boost/hof/pipable.hpp>
  12. #include <memory>
  13. #include "test.hpp"
  14. static constexpr auto add_one = BOOST_HOF_STATIC_LAMBDA(int x)
  15. {
  16. return x + 1;
  17. };
  18. template<class F>
  19. struct wrapper : F
  20. {
  21. BOOST_HOF_INHERIT_CONSTRUCTOR(wrapper, F)
  22. };
  23. template<class T>
  24. constexpr wrapper<T> wrap(T x)
  25. {
  26. return x;
  27. }
  28. BOOST_HOF_TEST_CASE()
  29. {
  30. BOOST_HOF_TEST_CHECK(3 == add_one(2));
  31. }
  32. BOOST_HOF_TEST_CASE()
  33. {
  34. constexpr auto add_one_again = add_one;
  35. BOOST_HOF_TEST_CHECK(3 == add_one_again(2));
  36. }
  37. BOOST_HOF_TEST_CASE()
  38. {
  39. constexpr auto add_one_again = wrap(add_one);
  40. BOOST_HOF_TEST_CHECK(3 == add_one_again(2));
  41. }
  42. namespace test_static {
  43. BOOST_HOF_STATIC_LAMBDA_FUNCTION(add_one) = [](int x)
  44. {
  45. return x + 1;
  46. };
  47. BOOST_HOF_TEST_CASE()
  48. {
  49. BOOST_HOF_TEST_CHECK(add_one(2) == 3);
  50. }
  51. BOOST_HOF_STATIC_LAMBDA_FUNCTION(sum_partial) = boost::hof::partial([](int x, int y)
  52. {
  53. return x + y;
  54. });
  55. BOOST_HOF_TEST_CASE()
  56. {
  57. #ifndef _MSC_VER
  58. STATIC_ASSERT_EMPTY(sum_partial);
  59. #endif
  60. BOOST_HOF_TEST_CHECK(3 == sum_partial(1, 2));
  61. BOOST_HOF_TEST_CHECK(3 == sum_partial(1)(2));
  62. }
  63. BOOST_HOF_STATIC_LAMBDA_FUNCTION(add_one_pipable) = boost::hof::pipable([](int x)
  64. {
  65. return x + 1;
  66. });
  67. BOOST_HOF_TEST_CASE()
  68. {
  69. #ifndef _MSC_VER
  70. STATIC_ASSERT_EMPTY(add_one_pipable);
  71. #endif
  72. BOOST_HOF_TEST_CHECK(3 == add_one_pipable(2));
  73. BOOST_HOF_TEST_CHECK(3 == (2 | add_one_pipable));
  74. }
  75. BOOST_HOF_STATIC_LAMBDA_FUNCTION(sum_infix) = boost::hof::infix([](int x, int y)
  76. {
  77. return x + y;
  78. });
  79. BOOST_HOF_TEST_CASE()
  80. {
  81. #ifndef _MSC_VER
  82. STATIC_ASSERT_EMPTY(sum_infix);
  83. #endif
  84. BOOST_HOF_TEST_CHECK(3 == (1 <sum_infix> 2));
  85. }
  86. }