Blame view

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