diff --git a/include/fc/static_variant.hpp b/include/fc/static_variant.hpp index 6c0ef5b..17c4453 100644 --- a/include/fc/static_variant.hpp +++ b/include/fc/static_variant.hpp @@ -245,6 +245,7 @@ std::vector> init_co template class static_variant { +protected: static_assert(impl::type_info::no_reference_types, "Reference types are not permitted in static_variant."); static_assert(impl::type_info::no_duplicates, "static_variant type arguments contain duplicate types."); @@ -269,7 +270,7 @@ class static_variant { new(storage.data()) X( std::move(x) ); } - void init(tag_type tag) + void init_from_tag(tag_type tag) { FC_ASSERT( tag >= 0 ); FC_ASSERT( tag < count() ); @@ -298,7 +299,7 @@ public: static_variant() { - init(0); + init_from_tag(0); } template @@ -428,7 +429,7 @@ public: FC_ASSERT( w >= 0 ); FC_ASSERT( w < count() ); clean(); - init(w); + init_from_tag(w); } tag_type which() const {return _tag;} diff --git a/tests/variant_test.cpp b/tests/variant_test.cpp index 4a66ede..12c66bc 100644 --- a/tests/variant_test.cpp +++ b/tests/variant_test.cpp @@ -46,6 +46,55 @@ FC_REFLECT( fc::test::item, (level)(w) ); BOOST_AUTO_TEST_SUITE(fc_variant_and_log) +BOOST_AUTO_TEST_CASE( types_edge_cases_test ) +{ + using namespace fc::test; + + class sv : public fc::static_variant<> + { + public: + BOOST_ATTRIBUTE_UNUSED typedef fc::static_variant<>::tag_type tag_type; + }; + + BOOST_TEST_MESSAGE( "========== Test empty static_variant ==========" ); + + BOOST_CHECK_THROW( fc::static_variant<>(), fc::assert_exception ); + + BOOST_TEST_MESSAGE( "========== Test static_variant with tag_type ==========" ); + + sv::tag_type init_value = 2; + fc::static_variant< sv::tag_type, std::string, fc::variant > variant_with_tagtype(init_value); + + BOOST_CHECK_EQUAL( variant_with_tagtype.count(), 3 ); + BOOST_CHECK_EQUAL( variant_with_tagtype.which(), 0 ); + + sv::tag_type current_value = variant_with_tagtype.get(); + BOOST_CHECK_EQUAL( current_value, init_value ); + BOOST_CHECK( variant_with_tagtype == init_value ); + + for (sv::tag_type i = variant_with_tagtype.count(); i-->0;) + { + variant_with_tagtype.set_which(i); + BOOST_CHECK_EQUAL(variant_with_tagtype.which(), i); + } + + BOOST_TEST_MESSAGE( "========== Test static_variant with static_variant ==========" ); + + using sv_double = fc::static_variant; + using sv_float = fc::static_variant; + fc::static_variant< sv_float, std::string, sv_double > variant; + sv_float variant_float = 1.5f; + variant = variant_float; + BOOST_CHECK_EQUAL( variant.which(), 0 ); + BOOST_CHECK_EQUAL( variant.get().get(), 1.5f ); + + sv_double variant_double = 1.0; + variant = variant_double; + BOOST_CHECK_EQUAL( variant.which() , 2); + BOOST_CHECK_EQUAL( variant.get().get(), 1.0 ); +} + + BOOST_AUTO_TEST_CASE( nested_objects_test ) { try {