mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
Span can be constructed from empty std::array safely (#686)
* Span std::array c'tor uses arr.data() instead of &arr[0] - Fixes runtime issues when constructing from an empty std::array * Construct span with std::data if C++17 detected * Specialize span c'tor for std::array of length 0, set storage to nullptr
This commit is contained in:
parent
2bf9f137a6
commit
c02ddae4bc
@ -394,17 +394,27 @@ public:
|
|||||||
: storage_(KnownNotNull{std::addressof(arr[0])}, details::extent_type<N>())
|
: storage_(KnownNotNull{std::addressof(arr[0])}, details::extent_type<N>())
|
||||||
{}
|
{}
|
||||||
|
|
||||||
template <std::size_t N, class ArrayElementType = std::remove_const_t<element_type>>
|
template <std::size_t N, class = std::enable_if_t<(N > 0)>>
|
||||||
// GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute // TODO: parser bug
|
constexpr span(std::array<std::remove_const_t<element_type>, N>& arr) noexcept
|
||||||
constexpr span(std::array<ArrayElementType, N>& arr) noexcept
|
: storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
|
||||||
: storage_(arr.data(), details::extent_type<N>())
|
{
|
||||||
{}
|
}
|
||||||
|
|
||||||
template <std::size_t N>
|
constexpr span(std::array<std::remove_const_t<element_type>, 0>&) noexcept
|
||||||
// GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute // TODO: parser bug
|
: storage_(static_cast<pointer>(nullptr), details::extent_type<0>())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::size_t N, class = std::enable_if_t<(N > 0)>>
|
||||||
constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) noexcept
|
constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) noexcept
|
||||||
: storage_(arr.data(), details::extent_type<N>())
|
: storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
|
||||||
{}
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr span(const std::array<std::remove_const_t<element_type>, 0>&) noexcept
|
||||||
|
: storage_(static_cast<pointer>(nullptr), details::extent_type<0>())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
// NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
|
// NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
|
||||||
// on Container to be a contiguous sequence container.
|
// on Container to be a contiguous sequence container.
|
||||||
|
@ -452,6 +452,12 @@ TEST_CASE("from_std_array_constructor")
|
|||||||
CHECK((cs.size() == narrow_cast<ptrdiff_t>(arr.size()) && cs.data() == arr.data()));
|
CHECK((cs.size() == narrow_cast<ptrdiff_t>(arr.size()) && cs.data() == arr.data()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::array<int, 0> empty_arr{};
|
||||||
|
span<int> s{empty_arr};
|
||||||
|
CHECK((s.size() == 0 && s.empty()));
|
||||||
|
}
|
||||||
|
|
||||||
std::array<AddressOverloaded, 4> ao_arr{};
|
std::array<AddressOverloaded, 4> ao_arr{};
|
||||||
|
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user