From a49ff1b8df978c1b80738cab3bb25b4140fa6424 Mon Sep 17 00:00:00 2001 From: "Jordan Maples [MSFT]" <49793787+JordanMaples@users.noreply.github.com> Date: Wed, 5 Feb 2020 17:12:31 -0800 Subject: [PATCH] update span specialization of at, change some tests back to int i with narrowing for the comparisons. --- include/gsl/span | 3 ++- tests/span_tests.cpp | 18 +++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/include/gsl/span b/include/gsl/span index ecbca5d..b922576 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -806,7 +806,8 @@ template constexpr ElementType& at(span s, index i) { // No bounds checking here because it is done in span::operator[] called below - return s[i]; + Ensures(i >= 0); + return s[narrow_cast(i)]; } // [span.obs] Free observer functions diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index 5d9c18b..790cee0 100644 --- a/tests/span_tests.cpp +++ b/tests/span_tests.cpp @@ -153,24 +153,24 @@ TEST(span_test, from_pointer_length_constructor) int arr[4] = {1, 2, 3, 4}; { - for (std::size_t i = 0; i < 4; ++i) + for (int i = 0; i < 4; ++i) { { - span s = {&arr[0], i}; - EXPECT_TRUE(s.size() == i); + span s = {&arr[0], narrow_cast(i)}; + EXPECT_TRUE(s.size() == narrow_cast(i)); EXPECT_TRUE(s.data() == &arr[0]); EXPECT_TRUE(s.empty() == (i == 0)); - for (std::size_t j = 0; j < i; ++j) - EXPECT_TRUE(arr[j] == s[j]); + for (int j = 0; j < i; ++j) + EXPECT_TRUE(arr[j] == s[narrow_cast(j)]); } { - span s = {&arr[i], 4 - i}; - EXPECT_TRUE(s.size() == 4 - i); + span s = {&arr[i], 4 - narrow_cast(i)}; + EXPECT_TRUE(s.size() == 4 - narrow_cast(i)); EXPECT_TRUE(s.data() == &arr[i]); EXPECT_TRUE(s.empty() == ((4 - i) == 0)); - for (std::size_t j = 0; j < 4 - i; ++j) - EXPECT_TRUE(arr[j + i] == s[j]); + for (int j = 0; j < 4 - i; ++j) + EXPECT_TRUE(arr[j + i] == s[narrow_cast(j)]); } } }