construct_from_tuple.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/mp11/detail/config.hpp>
  12. #include <boost/core/lightweight_test.hpp>
  13. #include <tuple>
  14. #include <memory>
  15. #include <utility>
  16. #include <array>
  17. struct T1
  18. {
  19. int x, y, z;
  20. T1( int x = 0, int y = 0, int z = 0 ): x(x), y(y), z(z) {}
  21. };
  22. struct T2
  23. {
  24. std::unique_ptr<int> x, y, z;
  25. T2( std::unique_ptr<int> x, std::unique_ptr<int> y, std::unique_ptr<int> z ): x(std::move(x)), y(std::move(y)), z(std::move(z)) {}
  26. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, <= 1800 )
  27. T2( T2&& r ): x( std::move(r.x) ), y( std::move(r.y) ), z( std::move(r.z) ) {}
  28. #endif
  29. };
  30. int main()
  31. {
  32. using boost::mp11::construct_from_tuple;
  33. {
  34. std::tuple<int, short, char> tp{ 1, 2, 3 };
  35. {
  36. T1 t1 = construct_from_tuple<T1>( tp );
  37. BOOST_TEST_EQ( t1.x, 1 );
  38. BOOST_TEST_EQ( t1.y, 2 );
  39. BOOST_TEST_EQ( t1.z, 3 );
  40. }
  41. {
  42. T1 t1 = construct_from_tuple<T1>( std::move(tp) );
  43. BOOST_TEST_EQ( t1.x, 1 );
  44. BOOST_TEST_EQ( t1.y, 2 );
  45. BOOST_TEST_EQ( t1.z, 3 );
  46. }
  47. }
  48. {
  49. std::tuple<int, short, char> const tp{ 1, 2, 3 };
  50. {
  51. T1 t1 = construct_from_tuple<T1>( tp );
  52. BOOST_TEST_EQ( t1.x, 1 );
  53. BOOST_TEST_EQ( t1.y, 2 );
  54. BOOST_TEST_EQ( t1.z, 3 );
  55. }
  56. {
  57. T1 t1 = construct_from_tuple<T1>( std::move(tp) );
  58. BOOST_TEST_EQ( t1.x, 1 );
  59. BOOST_TEST_EQ( t1.y, 2 );
  60. BOOST_TEST_EQ( t1.z, 3 );
  61. }
  62. }
  63. #if defined( __clang_major__ ) && __clang_major__ == 3 && __clang_minor__ < 8
  64. #else
  65. {
  66. 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)) };
  67. T2 t2 = construct_from_tuple<T2>( std::move(tp) );
  68. BOOST_TEST_EQ( *t2.x, 1 );
  69. BOOST_TEST_EQ( *t2.y, 2 );
  70. BOOST_TEST_EQ( *t2.z, 3 );
  71. }
  72. #endif
  73. {
  74. std::pair<int, short> tp{ 1, 2 };
  75. {
  76. T1 t1 = construct_from_tuple<T1>( tp );
  77. BOOST_TEST_EQ( t1.x, 1 );
  78. BOOST_TEST_EQ( t1.y, 2 );
  79. BOOST_TEST_EQ( t1.z, 0 );
  80. }
  81. {
  82. T1 t1 = construct_from_tuple<T1>( std::move(tp) );
  83. BOOST_TEST_EQ( t1.x, 1 );
  84. BOOST_TEST_EQ( t1.y, 2 );
  85. BOOST_TEST_EQ( t1.z, 0 );
  86. }
  87. }
  88. {
  89. std::pair<int, short> const tp{ 1, 2 };
  90. {
  91. T1 t1 = construct_from_tuple<T1>( tp );
  92. BOOST_TEST_EQ( t1.x, 1 );
  93. BOOST_TEST_EQ( t1.y, 2 );
  94. BOOST_TEST_EQ( t1.z, 0 );
  95. }
  96. {
  97. T1 t1 = construct_from_tuple<T1>( std::move(tp) );
  98. BOOST_TEST_EQ( t1.x, 1 );
  99. BOOST_TEST_EQ( t1.y, 2 );
  100. BOOST_TEST_EQ( t1.z, 0 );
  101. }
  102. }
  103. {
  104. std::array<int, 3> tp{{ 1, 2, 3 }};
  105. {
  106. T1 t1 = construct_from_tuple<T1>( tp );
  107. BOOST_TEST_EQ( t1.x, 1 );
  108. BOOST_TEST_EQ( t1.y, 2 );
  109. BOOST_TEST_EQ( t1.z, 3 );
  110. }
  111. {
  112. T1 t1 = construct_from_tuple<T1>( std::move(tp) );
  113. BOOST_TEST_EQ( t1.x, 1 );
  114. BOOST_TEST_EQ( t1.y, 2 );
  115. BOOST_TEST_EQ( t1.z, 3 );
  116. }
  117. }
  118. {
  119. std::array<int, 3> const tp{{ 1, 2, 3 }};
  120. {
  121. T1 t1 = construct_from_tuple<T1>( tp );
  122. BOOST_TEST_EQ( t1.x, 1 );
  123. BOOST_TEST_EQ( t1.y, 2 );
  124. BOOST_TEST_EQ( t1.z, 3 );
  125. }
  126. {
  127. T1 t1 = construct_from_tuple<T1>( std::move(tp) );
  128. BOOST_TEST_EQ( t1.x, 1 );
  129. BOOST_TEST_EQ( t1.y, 2 );
  130. BOOST_TEST_EQ( t1.z, 3 );
  131. }
  132. }
  133. {
  134. std::tuple<> tp;
  135. {
  136. T1 t1 = construct_from_tuple<T1>( tp );
  137. BOOST_TEST_EQ( t1.x, 0 );
  138. BOOST_TEST_EQ( t1.y, 0 );
  139. BOOST_TEST_EQ( t1.z, 0 );
  140. }
  141. {
  142. T1 t1 = construct_from_tuple<T1>( std::move(tp) );
  143. BOOST_TEST_EQ( t1.x, 0 );
  144. BOOST_TEST_EQ( t1.y, 0 );
  145. BOOST_TEST_EQ( t1.z, 0 );
  146. }
  147. }
  148. {
  149. std::array<int, 0> tp;
  150. {
  151. T1 t1 = construct_from_tuple<T1>( tp );
  152. BOOST_TEST_EQ( t1.x, 0 );
  153. BOOST_TEST_EQ( t1.y, 0 );
  154. BOOST_TEST_EQ( t1.z, 0 );
  155. }
  156. {
  157. T1 t1 = construct_from_tuple<T1>( std::move(tp) );
  158. BOOST_TEST_EQ( t1.x, 0 );
  159. BOOST_TEST_EQ( t1.y, 0 );
  160. BOOST_TEST_EQ( t1.z, 0 );
  161. }
  162. }
  163. return boost::report_errors();
  164. }