Additional std::array ctor to support const element cases.

This commit is contained in:
Neil MacIntosh 2016-06-14 20:14:17 -07:00
parent c94a66f468
commit 62f30205e5
2 changed files with 40 additions and 0 deletions

View File

@ -318,6 +318,11 @@ public:
{} {}
template <size_t N> template <size_t N>
constexpr span(std::array<element_type, N>& arr)
: storage_(&arr[0], extent_type<N>())
{}
template <size_t N, class = std::enable_if_t<is_const<element_type>::value>>
constexpr span(std::array<std::remove_const_t<element_type>, N>& arr) constexpr span(std::array<std::remove_const_t<element_type>, N>& arr)
: storage_(&arr[0], extent_type<N>()) : storage_(&arr[0], extent_type<N>())
{} {}

View File

@ -400,6 +400,7 @@ SUITE(span_tests)
take_a_span(get_an_array()); take_a_span(get_an_array());
} }
#endif #endif
{ {
auto get_an_array = []() -> std::array<int, 4> { return { 1, 2, 3, 4 }; }; auto get_an_array = []() -> std::array<int, 4> { return { 1, 2, 3, 4 }; };
auto take_a_span = [](span<const int> s) { static_cast<void>(s); }; auto take_a_span = [](span<const int> s) { static_cast<void>(s); };
@ -445,6 +446,40 @@ SUITE(span_tests)
#endif #endif
} }
TEST(from_std_array_const_constructor)
{
std::array<const int, 4> arr = {1, 2, 3, 4};
{
span<const int> s{arr};
CHECK(s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data());
}
{
span<const int, 4> s{arr};
CHECK(s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data());
}
#ifdef CONFIRM_COMPILATION_ERRORS
{
span<const int, 2> s{arr};
CHECK(s.size() == 2 && s.data() == arr.data());
}
{
span<const int, 0> s{arr};
CHECK(s.size() == 0 && s.data() == arr.data());
}
{
span<const int, 5> s{arr};
}
{
span<int, 4> s{arr};
}
#endif
}
TEST(from_container_constructor) TEST(from_container_constructor)
{ {
std::vector<int> v = {1, 2, 3}; std::vector<int> v = {1, 2, 3};