mirror of
https://github.com/boostorg/variant2.git
synced 2025-08-01 05:04:27 +02:00
get_if should return 0 when passed 0
This commit is contained in:
@@ -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;
|
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)
|
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
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// get
|
// get (type)
|
||||||
|
|
||||||
template<class U, class... T> constexpr U& get(variant<T...>& v)
|
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
|
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" );
|
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
|
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" );
|
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
|
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" );
|
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;
|
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
|
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" );
|
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;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@@ -196,5 +196,55 @@ int main()
|
|||||||
BOOST_TEST_EQ( get<2>(std::move(v)), 3.14f );
|
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();
|
return boost::report_errors();
|
||||||
}
|
}
|
||||||
|
@@ -162,5 +162,49 @@ int main()
|
|||||||
BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
|
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();
|
return boost::report_errors();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user