auto_visitors.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. //-----------------------------------------------------------------------------
  2. // boost-libs variant/test/auto_visitors.cpp source file
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. //-----------------------------------------------------------------------------
  5. //
  6. // Copyright (c) 2014-2019 Antony Polukhin
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See
  9. // accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #include "boost/config.hpp"
  12. #include "boost/core/lightweight_test.hpp"
  13. #include "boost/variant.hpp"
  14. #include "boost/variant/multivisitors.hpp"
  15. #include "boost/lexical_cast.hpp"
  16. #include <boost/noncopyable.hpp>
  17. #include <boost/core/ignore_unused.hpp>
  18. namespace has_result_type_tests {
  19. template <class T>
  20. struct wrap {
  21. typedef T result_type;
  22. };
  23. struct s1 : wrap<int> {};
  24. struct s2 : wrap<int&> {};
  25. struct s3 : wrap<const int&> {};
  26. struct s4 {};
  27. struct s5 : wrap<int*> {};
  28. struct s6 : wrap<int**> {};
  29. struct s7 : wrap<const int*> {};
  30. struct s8 : wrap<boost::noncopyable> {};
  31. struct s9 : wrap<boost::noncopyable&> {};
  32. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  33. struct s10 : wrap<boost::noncopyable&&> {};
  34. #endif
  35. struct s11 : wrap<const boost::noncopyable&> {};
  36. struct s12 : wrap<const boost::noncopyable*> {};
  37. struct s13 : wrap<boost::noncopyable*> {};
  38. struct s14 { typedef int result_type; };
  39. struct s15 { typedef int& result_type; };
  40. struct s16 { typedef const int& result_type; };
  41. }
  42. void test_has_result_type_triat() {
  43. using namespace has_result_type_tests;
  44. using boost::detail::variant::has_result_type;
  45. BOOST_TEST(has_result_type<s1>::value);
  46. BOOST_TEST(has_result_type<s2>::value);
  47. BOOST_TEST(has_result_type<s3>::value);
  48. BOOST_TEST(!has_result_type<s4>::value);
  49. BOOST_TEST(has_result_type<s5>::value);
  50. BOOST_TEST(has_result_type<s6>::value);
  51. BOOST_TEST(has_result_type<s7>::value);
  52. BOOST_TEST(has_result_type<s8>::value);
  53. BOOST_TEST(has_result_type<s9>::value);
  54. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  55. BOOST_TEST(has_result_type<s10>::value);
  56. #endif
  57. BOOST_TEST(has_result_type<s11>::value);
  58. BOOST_TEST(has_result_type<s12>::value);
  59. BOOST_TEST(has_result_type<s13>::value);
  60. BOOST_TEST(has_result_type<s14>::value);
  61. BOOST_TEST(has_result_type<s15>::value);
  62. BOOST_TEST(has_result_type<s16>::value);
  63. }
  64. struct lex_streamer_explicit: boost::static_visitor<std::string> {
  65. template <class T>
  66. const char* operator()(const T& ) {
  67. return "10";
  68. }
  69. template <class T1, class T2>
  70. const char* operator()(const T1& , const T2& ) {
  71. return "100";
  72. }
  73. };
  74. void run_explicit()
  75. {
  76. typedef boost::variant<int, std::string, double> variant_type;
  77. variant_type v2("10"), v1("100");
  78. lex_streamer_explicit visitor_ref;
  79. // Must return instance of std::string
  80. BOOST_TEST(boost::apply_visitor(visitor_ref, v2).c_str() == std::string("10"));
  81. BOOST_TEST(boost::apply_visitor(visitor_ref, v2, v1).c_str() == std::string("100"));
  82. }
  83. // Most part of tests from this file require decltype(auto)
  84. #ifdef BOOST_NO_CXX14_DECLTYPE_AUTO
  85. void run()
  86. {
  87. BOOST_TEST(true);
  88. }
  89. void run2()
  90. {
  91. BOOST_TEST(true);
  92. }
  93. void run3()
  94. {
  95. BOOST_TEST(true);
  96. }
  97. #else
  98. #include <iostream>
  99. struct lex_streamer {
  100. template <class T>
  101. std::string operator()(const T& val) const {
  102. return boost::lexical_cast<std::string>(val);
  103. }
  104. };
  105. struct lex_streamer_void {
  106. template <class T>
  107. void operator()(const T& val) const {
  108. std::cout << val << std::endl;
  109. }
  110. template <class T1, class T2>
  111. void operator()(const T1& val, const T2& val2) const {
  112. std::cout << val << '+' << val2 << std::endl;
  113. }
  114. template <class T1, class T2, class T3>
  115. void operator()(const T1& val, const T2& val2, const T3& val3) const {
  116. std::cout << val << '+' << val2 << '+' << val3 << std::endl;
  117. }
  118. };
  119. struct lex_streamer2 {
  120. std::string res;
  121. template <class T>
  122. const char* operator()(const T& /*val*/) const {
  123. return "fail";
  124. }
  125. template <class T1, class T2>
  126. const char* operator()(const T1& /*v1*/, const T2& /*v2*/) const {
  127. return "fail2";
  128. }
  129. template <class T1, class T2, class T3>
  130. const char* operator()(const T1& /*v1*/, const T2& /*v2*/, const T3& /*v3*/) const {
  131. return "fail3";
  132. }
  133. template <class T>
  134. std::string& operator()(const T& val) {
  135. res = boost::lexical_cast<std::string>(val);
  136. return res;
  137. }
  138. template <class T1, class T2>
  139. std::string& operator()(const T1& v1, const T2& v2) {
  140. res = boost::lexical_cast<std::string>(v1) + "+" + boost::lexical_cast<std::string>(v2);
  141. return res;
  142. }
  143. template <class T1, class T2, class T3>
  144. std::string& operator()(const T1& v1, const T2& v2, const T3& v3) {
  145. res = boost::lexical_cast<std::string>(v1) + "+" + boost::lexical_cast<std::string>(v2)
  146. + "+" + boost::lexical_cast<std::string>(v3);
  147. return res;
  148. }
  149. };
  150. #ifndef BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES
  151. # define BOOST_TEST_IF_HAS_VARIADIC(x) BOOST_TEST(x)
  152. #else
  153. # define BOOST_TEST_IF_HAS_VARIADIC(x) /**/
  154. #endif
  155. void run()
  156. {
  157. typedef boost::variant<int, std::string, double> variant_type;
  158. variant_type v1(1), v2("10"), v3(100.0);
  159. lex_streamer lex_streamer_visitor;
  160. BOOST_TEST(boost::apply_visitor(lex_streamer(), v1) == "1");
  161. BOOST_TEST_IF_HAS_VARIADIC(boost::apply_visitor(lex_streamer_visitor)(v1) == "1");
  162. BOOST_TEST(boost::apply_visitor(lex_streamer(), v2) == "10");
  163. BOOST_TEST_IF_HAS_VARIADIC(boost::apply_visitor(lex_streamer_visitor)(v2) == "10");
  164. #ifndef BOOST_NO_CXX14_GENERIC_LAMBDAS
  165. BOOST_TEST(boost::apply_visitor([](auto v) { return boost::lexical_cast<std::string>(v); }, v1) == "1");
  166. BOOST_TEST(boost::apply_visitor([](auto v) { return boost::lexical_cast<std::string>(v); }, v2) == "10");
  167. // Retun type must be the same in all instances, so this code does not compile
  168. //boost::variant<int, short, unsigned> v_diff_types(1);
  169. //BOOST_TEST(boost::apply_visitor([](auto v) { return v; }, v_diff_types) == 1);
  170. boost::apply_visitor([](auto v) { std::cout << v << std::endl; }, v1);
  171. boost::apply_visitor([](auto v) { std::cout << v << std::endl; }, v2);
  172. #endif
  173. lex_streamer2 visitor_ref;
  174. BOOST_TEST(boost::apply_visitor(visitor_ref, v1) == "1");
  175. BOOST_TEST(boost::apply_visitor(visitor_ref, v2) == "10");
  176. #ifndef BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES
  177. std::string& ref_to_string = boost::apply_visitor(visitor_ref, v1);
  178. BOOST_TEST(ref_to_string == "1");
  179. #endif
  180. lex_streamer_void lex_streamer_void_visitor;
  181. boost::apply_visitor(lex_streamer_void(), v1);
  182. boost::apply_visitor(lex_streamer_void(), v2);
  183. #ifndef BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES
  184. boost::apply_visitor(lex_streamer_void_visitor)(v2);
  185. #endif
  186. boost::ignore_unused(lex_streamer_visitor, visitor_ref, lex_streamer_void_visitor);
  187. }
  188. struct lex_combine {
  189. template <class T1, class T2>
  190. std::string operator()(const T1& v1, const T2& v2) const {
  191. return boost::lexical_cast<std::string>(v1) + "+" + boost::lexical_cast<std::string>(v2);
  192. }
  193. template <class T1, class T2, class T3>
  194. std::string operator()(const T1& v1, const T2& v2, const T3& v3) const {
  195. return boost::lexical_cast<std::string>(v1) + "+"
  196. + boost::lexical_cast<std::string>(v2) + '+'
  197. + boost::lexical_cast<std::string>(v3);
  198. }
  199. };
  200. void run2()
  201. {
  202. typedef boost::variant<int, std::string, double> variant_type;
  203. variant_type v1(1), v2("10"), v3(100.0);
  204. lex_combine lex_combine_visitor;
  205. BOOST_TEST(boost::apply_visitor(lex_combine(), v1, v2) == "1+10");
  206. BOOST_TEST(boost::apply_visitor(lex_combine(), v2, v1) == "10+1");
  207. BOOST_TEST_IF_HAS_VARIADIC(boost::apply_visitor(lex_combine_visitor)(v2, v1) == "10+1");
  208. #ifndef BOOST_NO_CXX14_GENERIC_LAMBDAS
  209. BOOST_TEST(
  210. boost::apply_visitor(
  211. [](auto v1, auto v2) {
  212. return boost::lexical_cast<std::string>(v1) + "+"
  213. + boost::lexical_cast<std::string>(v2);
  214. }
  215. , v1
  216. , v2
  217. ) == "1+10"
  218. );
  219. BOOST_TEST(
  220. boost::apply_visitor(
  221. [](auto v1, auto v2) {
  222. return boost::lexical_cast<std::string>(v1) + "+"
  223. + boost::lexical_cast<std::string>(v2);
  224. }
  225. , v2
  226. , v1
  227. ) == "10+1"
  228. );
  229. boost::apply_visitor([](auto v1, auto v2) { std::cout << v1 << '+' << v2 << std::endl; }, v1, v2);
  230. boost::apply_visitor([](auto v1, auto v2) { std::cout << v1 << '+' << v2 << std::endl; }, v2, v1);
  231. #endif
  232. lex_streamer2 visitor_ref;
  233. BOOST_TEST(boost::apply_visitor(visitor_ref, v1, v2) == "1+10");
  234. BOOST_TEST(boost::apply_visitor(visitor_ref, v2, v1) == "10+1");
  235. #ifndef BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES
  236. std::string& ref_to_string = boost::apply_visitor(visitor_ref)(v1, v2);
  237. BOOST_TEST(ref_to_string == "1+10");
  238. #endif
  239. boost::apply_visitor(lex_streamer_void(), v1, v2);
  240. boost::apply_visitor(lex_streamer_void(), v2, v1);
  241. boost::ignore_unused(lex_combine_visitor, visitor_ref);
  242. }
  243. #undef BOOST_TEST_IF_HAS_VARIADIC
  244. void run3()
  245. {
  246. #if !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_HDR_TUPLE)
  247. typedef boost::variant<int, std::string, double> variant_type;
  248. variant_type v1(1), v2("10"), v3(100);
  249. lex_combine lex_combine_visitor;
  250. BOOST_TEST(boost::apply_visitor(lex_combine(), v1, v2, v3) == "1+10+100");
  251. BOOST_TEST(boost::apply_visitor(lex_combine(), v2, v1, v3) == "10+1+100");
  252. BOOST_TEST(boost::apply_visitor(lex_combine_visitor)(v2, v1, v3) == "10+1+100");
  253. #ifndef BOOST_NO_CXX14_GENERIC_LAMBDAS
  254. BOOST_TEST(
  255. boost::apply_visitor(
  256. [](auto v1, auto v2, auto v3) {
  257. return boost::lexical_cast<std::string>(v1) + "+"
  258. + boost::lexical_cast<std::string>(v2) + "+"
  259. + boost::lexical_cast<std::string>(v3);
  260. }
  261. , v1
  262. , v2
  263. , v3
  264. ) == "1+10+100"
  265. );
  266. BOOST_TEST(
  267. boost::apply_visitor(
  268. [](auto v1, auto v2, auto v3) {
  269. return boost::lexical_cast<std::string>(v1) + "+"
  270. + boost::lexical_cast<std::string>(v2) + "+"
  271. + boost::lexical_cast<std::string>(v3);
  272. }
  273. , v3
  274. , v1
  275. , v3
  276. ) == "100+1+100"
  277. );
  278. boost::apply_visitor(
  279. [](auto v1, auto v2, auto v3) { std::cout << v1 << '+' << v2 << '+' << v3 << std::endl; },
  280. v1, v2, v3
  281. );
  282. boost::apply_visitor(
  283. [](auto v1, auto v2, auto v3) { std::cout << v1 << '+' << v2 << '+' << v3 << std::endl; },
  284. v2, v1, v3
  285. );
  286. #endif
  287. lex_streamer2 visitor_ref;
  288. BOOST_TEST(boost::apply_visitor(visitor_ref, v1, v2) == "1+10");
  289. BOOST_TEST(boost::apply_visitor(visitor_ref)(v2, v1) == "10+1");
  290. std::string& ref_to_string = boost::apply_visitor(visitor_ref, v1, v2);
  291. BOOST_TEST(ref_to_string == "1+10");
  292. lex_streamer_void lex_streamer_void_visitor;
  293. boost::apply_visitor(lex_streamer_void(), v1, v2, v1);
  294. boost::apply_visitor(lex_streamer_void(), v2, v1, v1);
  295. boost::apply_visitor(lex_streamer_void_visitor)(v2, v1, v1);
  296. #endif // !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_HDR_TUPLE)
  297. }
  298. #endif
  299. int main()
  300. {
  301. run_explicit();
  302. run();
  303. run2();
  304. run3();
  305. test_has_result_type_triat();
  306. return boost::report_errors();
  307. }