//----------------------------------------------------------------------------- // boost-libs variant/test/variant_reference_test.cpp source 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/variant.hpp" #include "boost/core/lightweight_test.hpp" #include "boost/mpl/bool.hpp" #include "boost/type_traits/add_reference.hpp" #include "boost/type_traits/is_pointer.hpp" ///// // support types and functions struct base_t { }; struct derived_t : base_t { }; template bool check_base_derived(Base* b, Derived* d, long) { return b == d; } template bool check_base_derived(Base& b, Derived& d, int) { return &b == &d; } template typename boost::add_reference::type wknd_get(boost::variant& var, long) { return boost::get(var); } template typename boost::add_reference::type wknd_get(boost::variant& var, int) { return boost::get(var); } ///// // test functions template void test_reference_content(T& t, const T& value1, const T& value2) { BOOST_TEST( !(value1 == value2) ); ///// boost::variant< T& > var(t); BOOST_TEST(( boost::get(&var) == &t )); t = value1; BOOST_TEST(( boost::get(var) == value1 )); ///// boost::variant< T > var2(var); BOOST_TEST(( boost::get(var2) == value1 )); t = value2; BOOST_TEST(( boost::get(var2) == value1 )); } template void base_derived_test(Derived d) { Base b(d); BOOST_TEST((check_base_derived( b , d , 1L ))); boost::variant base_var(d); BOOST_TEST((check_base_derived( wknd_get(base_var, 1L) , d , 1L ))); boost::variant derived_var(d); boost::variant base_from_derived_var(derived_var); BOOST_TEST((check_base_derived( wknd_get(base_from_derived_var, 1L) , wknd_get(derived_var, 1L) , 1L ))); } int main() { int i = 0; test_reference_content(i, 1, 2); ///// derived_t d; base_derived_test< int&,int >(i); base_derived_test< base_t*,derived_t* >(&d); base_derived_test< base_t&,derived_t& >(d); return boost::report_errors(); }