lazy_make_pair_tests.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. ////////////////////////////////////////////////////////////////////////////
  2. // lazy_make_pair_tests.cpp
  3. //
  4. // lazy make_pair test solving the optimizer problem.
  5. //
  6. ////////////////////////////////////////////////////////////////////////////
  7. /*=============================================================================
  8. Copyright (c) 2001-2007 Joel de Guzman
  9. Copyright (c) 2015 John Fletcher
  10. Distributed under the Boost Software License, Version 1.0. (See accompanying
  11. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  12. ==============================================================================*/
  13. #include <boost/phoenix/core/limits.hpp>
  14. #include <boost/detail/lightweight_test.hpp>
  15. #include <boost/phoenix/core.hpp>
  16. #include <boost/phoenix/function.hpp>
  17. namespace boost {
  18. namespace phoenix {
  19. #ifdef BOOST_RESULT_OF_USE_TR1
  20. namespace result_of {
  21. template <
  22. typename Arg1
  23. , typename Arg2
  24. >
  25. class make_pair
  26. {
  27. public:
  28. typedef typename boost::remove_reference<Arg1>::type Arg1Type;
  29. typedef typename boost::remove_reference<Arg2>::type Arg2Type;
  30. typedef std::pair<Arg1Type,Arg2Type> type;
  31. };
  32. }
  33. #endif
  34. namespace impl
  35. {
  36. struct make_pair {
  37. #ifdef BOOST_RESULT_OF_USE_TR1
  38. template <typename Sig>
  39. struct result;
  40. // This fails with -O2 unless refs are removed from A1 and A2.
  41. template <typename This, typename A0, typename A1>
  42. struct result<This(A0, A1)>
  43. {
  44. typedef typename result_of::make_pair<A0,A1>::type type;
  45. };
  46. #else
  47. template <typename Sig>
  48. struct result;
  49. template <typename This, typename A0, typename A1>
  50. struct result<This(A0, A1)>
  51. : boost::remove_reference<std::pair<A0, A1> >
  52. {};
  53. #endif
  54. template <typename A0, typename A1>
  55. #ifdef BOOST_RESULT_OF_USE_TR1
  56. typename result<make_pair(A0,A1)>::type
  57. #else
  58. std::pair<A0, A1>
  59. #endif
  60. operator()(A0 const & a0, A1 const & a1) const
  61. {
  62. return std::make_pair(a0,a1);
  63. }
  64. };
  65. }
  66. BOOST_PHOENIX_ADAPT_CALLABLE(make_pair, impl::make_pair, 2)
  67. }
  68. }
  69. int main()
  70. {
  71. namespace phx = boost::phoenix;
  72. using boost::phoenix::arg_names::arg1;
  73. using boost::phoenix::arg_names::arg2;
  74. int a = 99; int b = 256;
  75. std::pair<int,int> ab1 = phx::make_pair(a,b)();
  76. //std::cout << ab1.first << "," << ab1.second << std::endl;
  77. BOOST_TEST(ab1.first == 99 && ab1.second == 256);
  78. std::pair<int,int> ab2 = phx::make_pair(arg1,b)(a);
  79. //std::cout << ab2.first << "," << ab2.second << std::endl;
  80. BOOST_TEST(ab2.first == 99 && ab2.second == 256);
  81. std::pair<int,int> ab3 = phx::make_pair(arg1,arg2)(a,b);
  82. //std::cout << ab3.first << "," << ab3.second << std::endl;
  83. BOOST_TEST(ab3.first == 99 && ab3.second == 256);
  84. return boost::report_errors();
  85. }