test5.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //-----------------------------------------------------------------------------
  2. // boost-libs variant/test/test5.cpp header file
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. //-----------------------------------------------------------------------------
  5. //
  6. // Copyright (c) 2003
  7. // Eric Friedman, Itay Maman
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #include "boost/core/lightweight_test.hpp"
  13. #include "boost/variant.hpp"
  14. #include "jobs.h"
  15. #include <assert.h>
  16. #include <iostream>
  17. #include <string>
  18. #include <vector>
  19. void run()
  20. {
  21. using std::string;
  22. using boost::variant;
  23. using boost::apply_visitor;
  24. typedef variant<int, float, unsigned short, unsigned char> t_var1;
  25. typedef variant<int, t_var1, unsigned short, unsigned char> t_var2;
  26. typedef variant<string, int, t_var2> t_var3;
  27. t_var1 v1;
  28. t_var2 v2;
  29. t_var2 v2_second;
  30. t_var3 v3;
  31. const char c0 = 'x';
  32. v1 = c0;
  33. //v2 and v3 are holding (aka: containing) a variant
  34. v2 = v1;
  35. v3 = v2;
  36. verify(v1, spec<int>());
  37. verify(v2, spec<t_var1>());
  38. verify(v3, spec<t_var2>());
  39. //
  40. // assignment from const char (Converted to int)
  41. //
  42. v2 = c0;
  43. v3 = c0;
  44. verify(v2, spec<int>());
  45. verify(v3, spec<int>());
  46. BOOST_TEST(apply_visitor(sum_int(), v2) == c0);
  47. BOOST_TEST(apply_visitor(sum_int(), v3) == c0);
  48. sum_int adder;
  49. apply_visitor(adder, v2);
  50. apply_visitor(adder, v3);
  51. BOOST_TEST(adder.result() == 2*c0);
  52. //
  53. // A variant holding a variant
  54. //
  55. typedef variant<unsigned char, float> t_var4;
  56. typedef variant<string, t_var4> t_var5;
  57. t_var4 v4;
  58. t_var5 v5;
  59. v5 = 22.5f;
  60. verify(v5, spec<t_var4>(), "[V] [V] 22.5");
  61. }
  62. int main()
  63. {
  64. run();
  65. return boost::report_errors();
  66. }