diff --git a/include/fc/static_variant.hpp b/include/fc/static_variant.hpp index 46a96ca..3db2bdf 100644 --- a/include/fc/static_variant.hpp +++ b/include/fc/static_variant.hpp @@ -155,6 +155,7 @@ struct type_info { static const bool no_reference_types = false; static const bool no_duplicates = position::pos == -1 && type_info::no_duplicates; static const size_t size = type_info::size > sizeof(T&) ? type_info::size : sizeof(T&); + static const size_t count = 1 + type_info::count; }; template @@ -162,12 +163,14 @@ struct type_info { static const bool no_reference_types = type_info::no_reference_types; static const bool no_duplicates = position::pos == -1 && type_info::no_duplicates; static const size_t size = type_info::size > sizeof(T) ? type_info::size : sizeof(T&); + static const size_t count = 1 + type_info::count; }; template<> struct type_info<> { static const bool no_reference_types = true; static const bool no_duplicates = true; + static const size_t count = 0; static const size_t size = 0; }; @@ -314,13 +317,15 @@ public: return impl::storage_ops<0, Types...>::apply(_tag, storage, v); } - void set_which( int w ) { - this->~static_variant(); - _tag = w; - impl::storage_ops<0, Types...>::con(_tag, storage); - } + static int count() { return impl::type_info::count; } + void set_which( int w ) { + FC_ASSERT( w < count() ); + this->~static_variant(); + _tag = w; + impl::storage_ops<0, Types...>::con(_tag, storage); + } - int which() const {return _tag;} + int which() const {return _tag;} }; template @@ -369,4 +374,5 @@ struct visitor { s.visit( to_static_variant(ar[1]) ); } + template struct get_typename { static const char* name() { return typeid(static_variant).name(); } }; } // namespace fc