]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/variant/test/test8.cpp
1 //-----------------------------------------------------------------------------
2 // boost-libs variant/test/test8.cpp header file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
7 // Eric Friedman, Itay Maman
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)
13 #include "boost/test/minimal.hpp"
14 #include "boost/variant.hpp"
20 using namespace boost
;
22 typedef variant
<float, std::string
, int, std::vector
<std::string
> > t_var1
;
24 struct int_sum
: static_visitor
<>
26 int_sum() : result_(0) { }
28 void operator()(int t
)
33 result_type
operator()(float ) { }
34 result_type
operator()(const std::string
& ) { }
35 result_type
operator()(const std::vector
<std::string
>& ) { }
40 template <typename T
, typename Variant
>
41 T
& check_pass(Variant
& v
, T value
)
43 BOOST_CHECK(get
<T
>(&v
));
48 BOOST_CHECK(r
== value
);
51 catch(boost::bad_get
&)
53 throw; // must never reach
57 template <typename T
, typename Variant
>
58 void check_fail(Variant
& v
)
60 BOOST_CHECK(!relaxed_get
<T
>(&v
));
64 T
& r
= relaxed_get
<T
>(v
);
65 (void)r
; // suppress warning about r not being used
66 BOOST_CHECK(false && relaxed_get
<T
>(&v
)); // should never reach
68 catch(const boost::bad_get
& e
)
70 BOOST_CHECK(!!e
.what()); // make sure that what() is const qualified and returnes something
74 int test_main(int , char* [])
79 // check get on non-const variant
81 int& r1
= check_pass
<int>(v1
, 800);
82 const int& cr1
= check_pass
<const int>(v1
, 800);
84 check_fail
<float>(v1
);
85 check_fail
<const float>(v1
);
86 check_fail
<short>(v1
);
87 check_fail
<const short>(v1
);
89 apply_visitor(acc
, v1
);
90 BOOST_CHECK(acc
.result_
== 800);
92 r1
= 920; // NOTE: modifies content of v1
93 apply_visitor(acc
, v1
);
94 BOOST_CHECK(cr1
== 920);
95 BOOST_CHECK(acc
.result_
== 800 + 920);
98 // check const correctness:
100 const t_var1
& c
= v1
;
102 check_pass
<const int>(c
, 920);
104 //check_fail<int>(c);
105 check_fail
<const float>(c
);
106 //check_fail<float>(c);
107 check_fail
<const short>(c
);
108 //check_fail<short>(c);
111 return boost::exit_success
;