variant_get_by_type.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Copyright 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. #include <boost/variant2/variant.hpp>
  8. #include <boost/core/lightweight_test.hpp>
  9. #include <boost/core/lightweight_test_trait.hpp>
  10. #include <type_traits>
  11. #include <utility>
  12. #include <string>
  13. using namespace boost::variant2;
  14. int main()
  15. {
  16. {
  17. variant<int> v;
  18. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(v)), int&>));
  19. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(std::move(v))), int&&>));
  20. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int>(&v)), int*>));
  21. }
  22. {
  23. variant<int> const v;
  24. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(v)), int const&>));
  25. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(std::move(v))), int const&&>));
  26. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int>(&v)), int const*>));
  27. }
  28. {
  29. variant<int const> v;
  30. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(v)), int const&>));
  31. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(std::move(v))), int const&&>));
  32. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int const>(&v)), int const*>));
  33. }
  34. {
  35. variant<int volatile> const v;
  36. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int volatile>(v)), int const volatile&>));
  37. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int volatile>(std::move(v))), int const volatile&&>));
  38. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int volatile>(&v)), int const volatile*>));
  39. }
  40. {
  41. variant<int> v;
  42. BOOST_TEST_EQ( get<int>(v), 0 );
  43. BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
  44. BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
  45. }
  46. {
  47. variant<int> const v;
  48. BOOST_TEST_EQ( get<int>(v), 0 );
  49. BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
  50. BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
  51. }
  52. {
  53. variant<int> v( 1 );
  54. BOOST_TEST_EQ( get<int>(v), 1 );
  55. BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
  56. BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
  57. }
  58. {
  59. variant<int> const v( 1 );
  60. BOOST_TEST_EQ( get<int>(v), 1 );
  61. BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
  62. BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
  63. }
  64. {
  65. variant<int, float> v;
  66. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(v)), int&>));
  67. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(std::move(v))), int&&>));
  68. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int>(&v)), int*>));
  69. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float>(v)), float&>));
  70. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float>(std::move(v))), float&&>));
  71. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<float>(&v)), float*>));
  72. }
  73. {
  74. variant<int, float> const v;
  75. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(v)), int const&>));
  76. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int>(std::move(v))), int const&&>));
  77. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int>(&v)), int const*>));
  78. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float>(v)), float const&>));
  79. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float>(std::move(v))), float const&&>));
  80. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<float>(&v)), float const*>));
  81. }
  82. {
  83. variant<int const, float volatile> v;
  84. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(v)), int const&>));
  85. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(std::move(v))), int const&&>));
  86. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int const>(&v)), int const*>));
  87. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float volatile>(v)), float volatile&>));
  88. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float volatile>(std::move(v))), float volatile&&>));
  89. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<float volatile>(&v)), float volatile*>));
  90. }
  91. {
  92. variant<int const, float volatile> const v;
  93. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(v)), int const&>));
  94. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<int const>(std::move(v))), int const&&>));
  95. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<int const>(&v)), int const*>));
  96. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float volatile>(v)), float const volatile&>));
  97. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<float volatile>(std::move(v))), float const volatile&&>));
  98. BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<float volatile>(&v)), float const volatile*>));
  99. }
  100. {
  101. variant<int, float> v;
  102. BOOST_TEST_EQ( get<int>(v), 0 );
  103. BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
  104. BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
  105. BOOST_TEST_EQ( get_if<float>(&v), nullptr );
  106. BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
  107. }
  108. {
  109. variant<int, float> const v;
  110. BOOST_TEST_EQ( get<int>(v), 0 );
  111. BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
  112. BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
  113. BOOST_TEST_EQ( get_if<float>(&v), nullptr );
  114. BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
  115. }
  116. {
  117. variant<int, float> v( 1 );
  118. BOOST_TEST_EQ( get<int>(v), 1 );
  119. BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
  120. BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
  121. BOOST_TEST_EQ( get_if<float>(&v), nullptr );
  122. BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
  123. }
  124. {
  125. variant<int, float> const v( 1 );
  126. BOOST_TEST_EQ( get<int>(v), 1 );
  127. BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
  128. BOOST_TEST_THROWS( get<float>(v), bad_variant_access );
  129. BOOST_TEST_EQ( get_if<float>(&v), nullptr );
  130. BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
  131. }
  132. {
  133. variant<int, float> v( 3.14f );
  134. BOOST_TEST_THROWS( get<int>(v), bad_variant_access );
  135. BOOST_TEST_EQ( get_if<int>(&v), nullptr );
  136. BOOST_TEST_EQ( get<float>(v), 3.14f );
  137. BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
  138. BOOST_TEST_EQ( get<float>(std::move(v)), 3.14f );
  139. }
  140. {
  141. variant<int, float> const v( 3.14f );
  142. BOOST_TEST_THROWS( get<int>(v), bad_variant_access );
  143. BOOST_TEST_EQ( get_if<int>(&v), nullptr );
  144. BOOST_TEST_EQ( get<float>(v), 3.14f );
  145. BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
  146. BOOST_TEST_EQ( get<float>(std::move(v)), 3.14f );
  147. }
  148. {
  149. variant<int, float, float> v;
  150. BOOST_TEST_EQ( get<int>(v), 0 );
  151. BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
  152. BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
  153. }
  154. {
  155. variant<int, float, float> const v;
  156. BOOST_TEST_EQ( get<int>(v), 0 );
  157. BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
  158. BOOST_TEST_EQ( get<int>(std::move(v)), 0 );
  159. }
  160. {
  161. variant<int, float, float> v( 1 );
  162. BOOST_TEST_EQ( get<int>(v), 1 );
  163. BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
  164. BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
  165. }
  166. {
  167. variant<int, float, float> const v( 1 );
  168. BOOST_TEST_EQ( get<int>(v), 1 );
  169. BOOST_TEST_EQ( get_if<int>(&v), &get<int>(v) );
  170. BOOST_TEST_EQ( get<int>(std::move(v)), 1 );
  171. }
  172. {
  173. variant<int, int, float> v( 3.14f );
  174. BOOST_TEST_EQ( get<float>(v), 3.14f );
  175. BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
  176. BOOST_TEST_EQ( get<float>(std::move(v)), 3.14f );
  177. }
  178. {
  179. variant<int, int, float> const v( 3.14f );
  180. BOOST_TEST_EQ( get<float>(v), 3.14f );
  181. BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
  182. BOOST_TEST_EQ( get<float>(std::move(v)), 3.14f );
  183. }
  184. {
  185. variant<int> * p = 0;
  186. BOOST_TEST_EQ( get_if<int>(p), nullptr );
  187. }
  188. {
  189. variant<int const> * p = 0;
  190. BOOST_TEST_EQ( get_if<int const>(p), nullptr );
  191. }
  192. {
  193. variant<int> const * p = 0;
  194. BOOST_TEST_EQ( get_if<int>(p), nullptr );
  195. }
  196. {
  197. variant<int const> const * p = 0;
  198. BOOST_TEST_EQ( get_if<int const>(p), nullptr );
  199. }
  200. {
  201. variant<int, float> * p = 0;
  202. BOOST_TEST_EQ( get_if<int>(p), nullptr );
  203. BOOST_TEST_EQ( get_if<float>(p), nullptr );
  204. }
  205. {
  206. variant<int, float> const * p = 0;
  207. BOOST_TEST_EQ( get_if<int>(p), nullptr );
  208. BOOST_TEST_EQ( get_if<float>(p), nullptr );
  209. }
  210. {
  211. variant<int, int, float> * p = 0;
  212. BOOST_TEST_EQ( get_if<float>(p), nullptr );
  213. }
  214. {
  215. variant<int, int, float> const * p = 0;
  216. BOOST_TEST_EQ( get_if<float>(p), nullptr );
  217. }
  218. return boost::report_errors();
  219. }