From bdd449ae6a5ab8291763e8a84a2b3404c0fcaba0 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Fri, 2 Jun 2017 22:11:29 +0300 Subject: [PATCH] get_if should return 0 when passed 0 --- include/boost/variant2/variant.hpp | 12 +++---- test/variant_get_by_index.cpp | 50 ++++++++++++++++++++++++++++++ test/variant_get_by_type.cpp | 44 ++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 6 deletions(-) diff --git a/include/boost/variant2/variant.hpp b/include/boost/variant2/variant.hpp index cb669ad..7e78a6a 100644 --- a/include/boost/variant2/variant.hpp +++ b/include/boost/variant2/variant.hpp @@ -112,7 +112,7 @@ template constexpr bool holds_alternative( variant co return v.index() == mp_find, U>::value; } -// get +// get (index) template constexpr variant_alternative_t>& get(variant& v) { @@ -178,7 +178,7 @@ template constexpr variant_alternative_t constexpr U& get(variant& v) { @@ -253,13 +253,13 @@ template constexpr U const&& get(variant const&& v) template constexpr std::add_pointer_t>> get_if(variant* v) noexcept { static_assert( I < sizeof...(T), "Index out of bounds" ); - return v->index() == I? &v->_get_impl( mp_size_t() ): 0; + return v && v->index() == I? &v->_get_impl( mp_size_t() ): 0; } template constexpr std::add_pointer_t>> get_if(variant const * v) noexcept { static_assert( I < sizeof...(T), "Index out of bounds" ); - return v->index() == I? &v->_get_impl( mp_size_t() ): 0; + return v && v->index() == I? &v->_get_impl( mp_size_t() ): 0; } template constexpr std::add_pointer_t get_if(variant* v) noexcept @@ -267,7 +267,7 @@ template constexpr std::add_pointer_t get_if(variant, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); constexpr auto I = mp_find, U>::value; - return v->index() == I? &v->_get_impl( mp_size_t() ): 0; + return v && v->index() == I? &v->_get_impl( mp_size_t() ): 0; } template constexpr std::add_pointer_t get_if(variant const * v) noexcept @@ -275,7 +275,7 @@ template constexpr std::add_pointer_t get_if(varia static_assert( mp_count, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" ); constexpr auto I = mp_find, U>::value; - return v->index() == I? &v->_get_impl( mp_size_t() ): 0; + return v && v->index() == I? &v->_get_impl( mp_size_t() ): 0; } // diff --git a/test/variant_get_by_index.cpp b/test/variant_get_by_index.cpp index b55313e..950e73a 100644 --- a/test/variant_get_by_index.cpp +++ b/test/variant_get_by_index.cpp @@ -196,5 +196,55 @@ int main() BOOST_TEST_EQ( get<2>(std::move(v)), 3.14f ); } + { + variant * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant const * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant const * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant const * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant const * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + + { + variant const * p = 0; + BOOST_TEST_EQ( get_if<0>(p), nullptr ); + } + return boost::report_errors(); } diff --git a/test/variant_get_by_type.cpp b/test/variant_get_by_type.cpp index 82b18c7..5a179dc 100644 --- a/test/variant_get_by_type.cpp +++ b/test/variant_get_by_type.cpp @@ -162,5 +162,49 @@ int main() BOOST_TEST_EQ( get_if(&v), &get(v) ); } + { + variant * p = 0; + BOOST_TEST_EQ( get_if(p), nullptr ); + } + + { + variant * p = 0; + BOOST_TEST_EQ( get_if(p), nullptr ); + } + + { + variant const * p = 0; + BOOST_TEST_EQ( get_if(p), nullptr ); + } + + { + variant const * p = 0; + BOOST_TEST_EQ( get_if(p), nullptr ); + } + + { + variant * p = 0; + + BOOST_TEST_EQ( get_if(p), nullptr ); + BOOST_TEST_EQ( get_if(p), nullptr ); + } + + { + variant const * p = 0; + + BOOST_TEST_EQ( get_if(p), nullptr ); + BOOST_TEST_EQ( get_if(p), nullptr ); + } + + { + variant * p = 0; + BOOST_TEST_EQ( get_if(p), nullptr ); + } + + { + variant const * p = 0; + BOOST_TEST_EQ( get_if(p), nullptr ); + } + return boost::report_errors(); }