tuple_apply.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2015, 2017 Peter Dimov.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. //
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. #if defined(_MSC_VER)
  8. #pragma warning( disable: 4244 ) // 'initializing': conversion from 'int' to 'char', possible loss of data
  9. #endif
  10. #include <boost/mp11/tuple.hpp>
  11. #include <boost/core/lightweight_test.hpp>
  12. #include <tuple>
  13. #include <memory>
  14. #include <utility>
  15. #include <array>
  16. int main()
  17. {
  18. using boost::mp11::tuple_apply;
  19. {
  20. std::tuple<int, short, char> tp{ 1, 2, 3 };
  21. {
  22. int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, tp );
  23. BOOST_TEST_EQ( s, 123 );
  24. }
  25. {
  26. int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, std::move(tp) );
  27. BOOST_TEST_EQ( s, 123 );
  28. }
  29. }
  30. {
  31. std::tuple<int, short, char> const tp{ 1, 2, 3 };
  32. {
  33. int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, tp );
  34. BOOST_TEST_EQ( s, 123 );
  35. }
  36. {
  37. int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, std::move(tp) );
  38. BOOST_TEST_EQ( s, 123 );
  39. }
  40. }
  41. #if defined( __clang_major__ ) && __clang_major__ == 3 && __clang_minor__ < 8
  42. #else
  43. {
  44. std::tuple<std::unique_ptr<int>, std::unique_ptr<int>, std::unique_ptr<int>> tp{ std::unique_ptr<int>(new int(1)), std::unique_ptr<int>(new int(2)), std::unique_ptr<int>(new int(3)) };
  45. int s = tuple_apply( [&]( std::unique_ptr<int> px, std::unique_ptr<int> py, std::unique_ptr<int> pz ){ return 100 * *px + 10 * *py + *pz; }, std::move(tp) );
  46. BOOST_TEST_EQ( s, 123 );
  47. }
  48. #endif
  49. {
  50. std::pair<int, short> tp{ 1, 2 };
  51. {
  52. int s = tuple_apply( [&]( int x, int y ){ return 10 * x + y; }, tp );
  53. BOOST_TEST_EQ( s, 12 );
  54. }
  55. {
  56. int s = tuple_apply( [&]( int x, int y ){ return 10 * x + y; }, std::move(tp) );
  57. BOOST_TEST_EQ( s, 12 );
  58. }
  59. }
  60. {
  61. std::pair<int, short> const tp{ 1, 2 };
  62. {
  63. int s = tuple_apply( [&]( int x, int y ){ return 10 * x + y; }, tp );
  64. BOOST_TEST_EQ( s, 12 );
  65. }
  66. {
  67. int s = tuple_apply( [&]( int x, int y ){ return 10 * x + y; }, std::move(tp) );
  68. BOOST_TEST_EQ( s, 12 );
  69. }
  70. }
  71. {
  72. std::array<int, 3> tp{{ 1, 2, 3 }};
  73. {
  74. int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, tp );
  75. BOOST_TEST_EQ( s, 123 );
  76. }
  77. {
  78. int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, std::move(tp) );
  79. BOOST_TEST_EQ( s, 123 );
  80. }
  81. }
  82. {
  83. std::array<int, 3> const tp{{ 1, 2, 3 }};
  84. {
  85. int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, tp );
  86. BOOST_TEST_EQ( s, 123 );
  87. }
  88. {
  89. int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, std::move(tp) );
  90. BOOST_TEST_EQ( s, 123 );
  91. }
  92. }
  93. {
  94. std::tuple<> tp;
  95. BOOST_TEST_EQ( tuple_apply( []{ return 11; }, tp ), 11 );
  96. BOOST_TEST_EQ( tuple_apply( []{ return 12; }, std::move( tp ) ), 12 );
  97. }
  98. {
  99. std::array<int, 0> tp;
  100. BOOST_TEST_EQ( tuple_apply( []{ return 11; }, tp ), 11 );
  101. BOOST_TEST_EQ( tuple_apply( []{ return 12; }, std::move( tp ) ), 12 );
  102. }
  103. return boost::report_errors();
  104. }