1919// thanks to Robert Ramey and Peter Dimov.
2020//
2121
22+ #include < boost/serialization/variant.hpp>
23+
2224#include < cstddef> // NULL
2325#include < cstdio> // remove
2426#include < fstream>
@@ -48,16 +50,22 @@ namespace std {
4850#include < boost/serialization/nvp.hpp>
4951#include < boost/serialization/throw_exception.hpp>
5052
53+ #include < boost/variant/variant.hpp>
54+ #include < boost/variant/apply_visitor.hpp>
5155#include < boost/variant/static_visitor.hpp>
5256
53- namespace boost {
54- template <typename ResultType> class static_visitor ;
55- }
57+ #if BOOST_CXX_VERSION >= 201103L
58+ # include < boost/variant2/variant.hpp>
59+ #endif
60+
61+ #ifndef BOOST_NO_CXX17_HDR_VARIANT
62+ # include < variant>
63+ #endif
5664
5765#include " A.hpp"
5866#include " A.ipp"
5967
60- class are_equal
68+ class are_equal_vis
6169 : public boost::static_visitor<bool >
6270{
6371public:
@@ -112,6 +120,29 @@ class are_equal
112120 }
113121};
114122
123+ template <class ... T> bool are_equal ( boost::variant<T...> const & v1, boost::variant<T...> const & v2 )
124+ {
125+ return boost::apply_visitor ( are_equal_vis (), v1, v2 );
126+ }
127+
128+ #if BOOST_CXX_VERSION >= 201103L
129+
130+ template <class ... T> bool are_equal ( boost::variant2::variant<T...> const & v1, boost::variant2::variant<T...> const & v2 )
131+ {
132+ return boost::variant2::visit ( are_equal_vis (), v1, v2 );
133+ }
134+
135+ #endif
136+
137+ #ifndef BOOST_NO_CXX17_HDR_VARIANT
138+
139+ template <class ... T> bool are_equal ( std::variant<T...> const & v1, std::variant<T...> const & v2 )
140+ {
141+ return std::visit ( are_equal_vis (), v1, v2 );
142+ }
143+
144+ #endif
145+
115146template <class Variant >
116147bool test_type (const Variant & v){
117148 const char * testfile = boost::archive::tmpnam (NULL );
@@ -128,7 +159,7 @@ bool test_type(const Variant & v){
128159 test_iarchive ia (is, TEST_ARCHIVE_FLAGS );
129160 BOOST_TRY {
130161 ia >> boost::serialization::make_nvp (" written" , vx);
131- BOOST_CHECK (visit ( are_equal (), v, vx));
162+ BOOST_CHECK (are_equal (v, vx));
132163 }
133164 BOOST_CATCH (boost::archive::archive_exception const & e) {
134165 return false ;
@@ -158,12 +189,6 @@ void test(Variant & v)
158189 test_type (v);
159190}
160191
161- #include < boost/serialization/variant.hpp>
162-
163- #include < boost/variant/variant.hpp>
164-
165- #include < cstdio>
166-
167192int test_boost_variant (){
168193 std::cerr << " Testing boost_variant\n " ;
169194 boost::variant<bool , int , float , double , A, std::string> v;
@@ -176,7 +201,6 @@ int test_boost_variant(){
176201
177202// boost::variant2/variant requires C++ 11
178203#if BOOST_CXX_VERSION >= 201103L
179- #include < boost/variant2/variant.hpp>
180204
181205int test_boost_variant2 (){
182206 std::cerr << " Testing boost_variant2\n " ;
@@ -191,7 +215,7 @@ int test_boost_variant2(){
191215
192216// std::variant reqires C++ 17 or more
193217#ifndef BOOST_NO_CXX17_HDR_VARIANT
194- # include < variant >
218+
195219int test_std_variant (){
196220 std::cerr << " Testing Std Variant\n " ;
197221 std::variant<bool , int , float , double , A, std::string> v;
0 commit comments