From adeb970be600050d301264106ad431873acaeee0 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Mon, 3 Jun 2019 01:05:15 +0300 Subject: [PATCH] Introduce detail::throw_bad_variant_access --- include/boost/variant2/variant.hpp | 36 +++++++++++++++++++----------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/include/boost/variant2/variant.hpp b/include/boost/variant2/variant.hpp index 4b2110d..ab7fcab 100644 --- a/include/boost/variant2/variant.hpp +++ b/include/boost/variant2/variant.hpp @@ -48,6 +48,16 @@ public: } }; +namespace detail +{ + +BOOST_NORETURN inline void throw_bad_variant_access() +{ + throw bad_variant_access(); +} + +} // namespace detail + // monostate struct monostate @@ -283,7 +293,7 @@ template constexpr bool holds_alternative( variant co template constexpr variant_alternative_t>& get(variant& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); - return (void)( v.index() != I? throw bad_variant_access(): 0 ), v._get_impl( mp11::mp_size_t() ); + return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( mp11::mp_size_t() ); } template constexpr variant_alternative_t>&& get(variant&& v) @@ -292,11 +302,11 @@ template constexpr variant_alternative_t() ) ); + return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( mp11::mp_size_t() ) ); #else - if( v.index() != I ) throw bad_variant_access(); + if( v.index() != I ) detail::throw_bad_variant_access(); return std::move( v._get_impl( mp11::mp_size_t() ) ); #endif @@ -305,7 +315,7 @@ template constexpr variant_alternative_t constexpr variant_alternative_t> const& get(variant const& v) { static_assert( I < sizeof...(T), "Index out of bounds" ); - return (void)( v.index() != I? throw bad_variant_access(): 0 ), v._get_impl( mp11::mp_size_t() ); + return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( mp11::mp_size_t() ); } template constexpr variant_alternative_t> const&& get(variant const&& v) @@ -314,11 +324,11 @@ template constexpr variant_alternative_t() ) ); + return ( v.index() != I? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( mp11::mp_size_t() ) ); #else - if( v.index() != I ) throw bad_variant_access(); + if( v.index() != I ) detail::throw_bad_variant_access(); return std::move( v._get_impl( mp11::mp_size_t() ) ); #endif @@ -363,7 +373,7 @@ template constexpr U& get(variant& v) using I = mp11::mp_find, U>; - return (void)( v.index() != I::value? throw bad_variant_access(): 0 ), v._get_impl( I() ); + return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( I() ); } template constexpr U&& get(variant&& v) @@ -374,11 +384,11 @@ template constexpr U&& get(variant&& v) #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930) - return (void)( v.index() != I::value? throw bad_variant_access(): 0 ), std::move( v._get_impl( I() ) ); + return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( I() ) ); #else - if( v.index() != I::value ) throw bad_variant_access(); + if( v.index() != I::value ) detail::throw_bad_variant_access(); return std::move( v._get_impl( I() ) ); #endif @@ -390,7 +400,7 @@ template constexpr U const& get(variant const& v) using I = mp11::mp_find, U>; - return (void)( v.index() != I::value? throw bad_variant_access(): 0 ), v._get_impl( I() ); + return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), v._get_impl( I() ); } template constexpr U const&& get(variant const&& v) @@ -401,11 +411,11 @@ template constexpr U const&& get(variant const&& v) #if !BOOST_WORKAROUND(BOOST_MSVC, < 1930) - return (void)( v.index() != I::value? throw bad_variant_access(): 0 ), std::move( v._get_impl( I() ) ); + return ( v.index() != I::value? detail::throw_bad_variant_access(): (void)0 ), std::move( v._get_impl( I() ) ); #else - if( v.index() != I::value ) throw bad_variant_access(); + if( v.index() != I::value ) detail::throw_bad_variant_access(); return std::move( v._get_impl( I() ) ); #endif @@ -1373,7 +1383,7 @@ private: template static variant _subset_impl( mp11::mp_size_t, V && /*v*/ ) { - throw bad_variant_access(); + detail::throw_bad_variant_access(); } private: