update span specialization of at, change some tests back to int i with narrowing for the comparisons.

This commit is contained in:
Jordan Maples [MSFT] 2020-02-05 17:12:31 -08:00
parent 3b9d15f49f
commit a49ff1b8df
2 changed files with 11 additions and 10 deletions

View File

@ -806,7 +806,8 @@ template <class ElementType, std::size_t Extent>
constexpr ElementType& at(span<ElementType, Extent> s, index i) constexpr ElementType& at(span<ElementType, Extent> s, index i)
{ {
// No bounds checking here because it is done in span::operator[] called below // No bounds checking here because it is done in span::operator[] called below
return s[i]; Ensures(i >= 0);
return s[narrow_cast<std::size_t>(i)];
} }
// [span.obs] Free observer functions // [span.obs] Free observer functions

View File

@ -153,24 +153,24 @@ TEST(span_test, from_pointer_length_constructor)
int arr[4] = {1, 2, 3, 4}; int arr[4] = {1, 2, 3, 4};
{ {
for (std::size_t i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)
{ {
{ {
span<int> s = {&arr[0], i}; span<int> s = {&arr[0], narrow_cast<std::size_t>(i)};
EXPECT_TRUE(s.size() == i); EXPECT_TRUE(s.size() == narrow_cast<std::size_t>(i));
EXPECT_TRUE(s.data() == &arr[0]); EXPECT_TRUE(s.data() == &arr[0]);
EXPECT_TRUE(s.empty() == (i == 0)); EXPECT_TRUE(s.empty() == (i == 0));
for (std::size_t j = 0; j < i; ++j) for (int j = 0; j < i; ++j)
EXPECT_TRUE(arr[j] == s[j]); EXPECT_TRUE(arr[j] == s[narrow_cast<std::size_t>(j)]);
} }
{ {
span<int> s = {&arr[i], 4 - i}; span<int> s = {&arr[i], 4 - narrow_cast<std::size_t>(i)};
EXPECT_TRUE(s.size() == 4 - i); EXPECT_TRUE(s.size() == 4 - narrow_cast<std::size_t>(i));
EXPECT_TRUE(s.data() == &arr[i]); EXPECT_TRUE(s.data() == &arr[i]);
EXPECT_TRUE(s.empty() == ((4 - i) == 0)); EXPECT_TRUE(s.empty() == ((4 - i) == 0));
for (std::size_t j = 0; j < 4 - i; ++j) for (int j = 0; j < 4 - i; ++j)
EXPECT_TRUE(arr[j + i] == s[j]); EXPECT_TRUE(arr[j + i] == s[narrow_cast<std::size_t>(j)]);
} }
} }
} }