1 // Copyright (c) 2017 2 // Mikhail Maximov 3 // 4 // Distributed under the Boost Software License, Version 1.0. (See 5 // accompanying file LICENSE_1_0.txt or copy at 6 // http://www.boost.org/LICENSE_1_0.txt) 7 8 // The test is base on https://svn.boost.org/trac/boost/ticket/8554 9 // variant was not able to extract types from mpl::joint_view 10 11 #include <string> 12 13 #include "boost/config.hpp" 14 #include "boost/core/lightweight_test.hpp" 15 16 #include "boost/variant.hpp" 17 #include "boost/mpl/joint_view.hpp" 18 #include "boost/mpl/insert_range.hpp" 19 #include "boost/mpl/set.hpp" 20 21 template<class T, class Variant> check_exception_on_get(Variant & v)22void check_exception_on_get(Variant& v) { 23 try { 24 boost::get<T>(v); 25 BOOST_ERROR("Expected exception boost::bad_get, but got nothing."); 26 } catch (boost::bad_get&) { //okay it is expected behaviour 27 } catch (...) { BOOST_ERROR("Expected exception boost::bad_get, but got something else."); } 28 } 29 test_joint_view()30void test_joint_view() { 31 typedef boost::variant<int> v1; 32 typedef boost::variant<std::string> v2; 33 typedef boost::make_variant_over<boost::mpl::joint_view<v1::types, v2::types>::type>::type v3; 34 35 v1 a = 1; 36 v2 b = "2"; 37 v3 c = a; 38 BOOST_TEST(boost::get<int>(c) == 1); 39 BOOST_TEST(c.which() == 0); 40 v3 d = b; 41 BOOST_TEST(boost::get<std::string>(d) == "2"); 42 BOOST_TEST(d.which() == 1); 43 check_exception_on_get<std::string>(c); 44 check_exception_on_get<int>(d); 45 } 46 test_set()47void test_set() { 48 typedef boost::mpl::set2< std::string, int > types; 49 typedef boost::make_variant_over< types >::type v; 50 51 v a = 1; 52 BOOST_TEST(boost::get<int>(a) == 1); 53 check_exception_on_get<std::string>(a); 54 a = "2"; 55 BOOST_TEST(boost::get<std::string>(a) == "2"); 56 check_exception_on_get<int>(a); 57 } 58 main()59int main() 60 { 61 test_joint_view(); 62 test_set(); 63 return boost::report_errors(); 64 } 65