get_if should return 0 when passed 0

This commit is contained in:
Peter Dimov
2017-06-02 22:11:29 +03:00
parent c867329bb8
commit bdd449ae6a
3 changed files with 100 additions and 6 deletions

View File

@ -112,7 +112,7 @@ template<class U, class... T> constexpr bool holds_alternative( variant<T...> co
return v.index() == mp_find<variant<T...>, U>::value;
}
// get
// get (index)
template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>>& get(variant<T...>& v)
{
@ -178,7 +178,7 @@ template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T
#endif
}
// get
// get (type)
template<class U, class... T> constexpr U& get(variant<T...>& v)
{
@ -253,13 +253,13 @@ template<class U, class... T> constexpr U const&& get(variant<T...> const&& v)
template<std::size_t I, class... T> constexpr std::add_pointer_t<variant_alternative_t<I, variant<T...>>> get_if(variant<T...>* v) noexcept
{
static_assert( I < sizeof...(T), "Index out of bounds" );
return v->index() == I? &v->_get_impl( mp_size_t<I>() ): 0;
return v && v->index() == I? &v->_get_impl( mp_size_t<I>() ): 0;
}
template<std::size_t I, class... T> constexpr std::add_pointer_t<const variant_alternative_t<I, variant<T...>>> get_if(variant<T...> const * v) noexcept
{
static_assert( I < sizeof...(T), "Index out of bounds" );
return v->index() == I? &v->_get_impl( mp_size_t<I>() ): 0;
return v && v->index() == I? &v->_get_impl( mp_size_t<I>() ): 0;
}
template<class U, class... T> constexpr std::add_pointer_t<U> get_if(variant<T...>* v) noexcept
@ -267,7 +267,7 @@ template<class U, class... T> constexpr std::add_pointer_t<U> get_if(variant<T..
static_assert( mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
constexpr auto I = mp_find<variant<T...>, U>::value;
return v->index() == I? &v->_get_impl( mp_size_t<I>() ): 0;
return v && v->index() == I? &v->_get_impl( mp_size_t<I>() ): 0;
}
template<class U, class... T> constexpr std::add_pointer_t<U const> get_if(variant<T...> const * v) noexcept
@ -275,7 +275,7 @@ template<class U, class... T> constexpr std::add_pointer_t<U const> get_if(varia
static_assert( mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
constexpr auto I = mp_find<variant<T...>, U>::value;
return v->index() == I? &v->_get_impl( mp_size_t<I>() ): 0;
return v && v->index() == I? &v->_get_impl( mp_size_t<I>() ): 0;
}
//

View File

@ -196,5 +196,55 @@ int main()
BOOST_TEST_EQ( get<2>(std::move(v)), 3.14f );
}
{
variant<int> * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int const> * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int> const * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int const> const * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int, float> * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int, float> const * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int const, float volatile> * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int const, float volatile> const * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int, int, float, float> * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int, int, float, float> const * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
return boost::report_errors();
}

View File

@ -162,5 +162,49 @@ int main()
BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
}
{
variant<int> * p = 0;
BOOST_TEST_EQ( get_if<int>(p), nullptr );
}
{
variant<int const> * p = 0;
BOOST_TEST_EQ( get_if<int const>(p), nullptr );
}
{
variant<int> const * p = 0;
BOOST_TEST_EQ( get_if<int>(p), nullptr );
}
{
variant<int const> const * p = 0;
BOOST_TEST_EQ( get_if<int const>(p), nullptr );
}
{
variant<int, float> * p = 0;
BOOST_TEST_EQ( get_if<int>(p), nullptr );
BOOST_TEST_EQ( get_if<float>(p), nullptr );
}
{
variant<int, float> const * p = 0;
BOOST_TEST_EQ( get_if<int>(p), nullptr );
BOOST_TEST_EQ( get_if<float>(p), nullptr );
}
{
variant<int, int, float> * p = 0;
BOOST_TEST_EQ( get_if<float>(p), nullptr );
}
{
variant<int, int, float> const * p = 0;
BOOST_TEST_EQ( get_if<float>(p), nullptr );
}
return boost::report_errors();
}