From 3b9d15f49fe043d42b2715dc5041f23ed7f56c5c Mon Sep 17 00:00:00 2001 From: "Jordan Maples [MSFT]" <49793787+JordanMaples@users.noreply.github.com> Date: Wed, 5 Feb 2020 17:02:23 -0800 Subject: [PATCH] reverting changes to gsl::index --- include/gsl/gsl_util | 13 +++++++------ tests/at_tests.cpp | 26 +++++++++++++++++--------- tests/utils_tests.cpp | 2 +- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/include/gsl/gsl_util b/include/gsl/gsl_util index 53141e6..d1f7f33 100644 --- a/include/gsl/gsl_util +++ b/include/gsl/gsl_util @@ -49,7 +49,7 @@ namespace gsl // // index type for all container indexes/subscripts/sizes -using index = std::size_t; +using index = std::ptrdiff_t; // final_action allows you to ensure something gets run at the end of a scope template @@ -129,8 +129,8 @@ GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute constexpr T& at(T (&arr)[N], const index i) { - Expects(i >= 0 && i < N); - return arr[i]; + Expects(i >= 0 && i < narrow_cast(N)); + return arr[narrow_cast(i)]; } template @@ -138,15 +138,16 @@ GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute constexpr auto at(Cont& cont, const index i) -> decltype(cont[cont.size()]) { - Expects(i >= 0 && i < cont.size()); - return cont[i]; + Expects(i >= 0 && i < narrow_cast(cont.size())); + using size_type = decltype(cont.size()); + return cont[narrow_cast(i)]; } template GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute constexpr T at(const std::initializer_list cont, const index i) { - Expects(i >= 0 && i < cont.size()); + Expects(i >= 0 && i < narrow_cast(cont.size())); return *(cont.begin() + i); } diff --git a/tests/at_tests.cpp b/tests/at_tests.cpp index 0580301..be2c7b8 100644 --- a/tests/at_tests.cpp +++ b/tests/at_tests.cpp @@ -33,7 +33,7 @@ TEST(at_tests, static_array) int a[4] = {1, 2, 3, 4}; const int(&c_a)[4] = a; - for (std::size_t i = 0; i < 4; ++i) { + for (int i = 0; i < 4; ++i) { EXPECT_TRUE(&gsl::at(a, i) == &a[i]); EXPECT_TRUE(&gsl::at(c_a, i) == &a[i]); } @@ -43,7 +43,9 @@ TEST(at_tests, static_array) std::abort(); }); + EXPECT_DEATH(gsl::at(a, -1), deathstring); EXPECT_DEATH(gsl::at(a, 4), deathstring); + EXPECT_DEATH(gsl::at(c_a, -1), deathstring); EXPECT_DEATH(gsl::at(c_a, 4), deathstring); } @@ -52,7 +54,7 @@ TEST(at_tests, std_array) std::array a = {1, 2, 3, 4}; const std::array& c_a = a; - for (std::size_t i = 0; i < 4; ++i) { + for (int i = 0; i < 4; ++i) { EXPECT_TRUE(&gsl::at(a, i) == &a[static_cast(i)]); EXPECT_TRUE(&gsl::at(c_a, i) == &a[static_cast(i)]); } @@ -62,7 +64,9 @@ TEST(at_tests, std_array) std::abort(); }); + EXPECT_DEATH(gsl::at(a, -1), deathstring); EXPECT_DEATH(gsl::at(a, 4), deathstring); + EXPECT_DEATH(gsl::at(c_a, -1), deathstring); EXPECT_DEATH(gsl::at(c_a, 4), deathstring); } @@ -71,7 +75,7 @@ TEST(at_tests, std_vector) std::vector a = {1, 2, 3, 4}; const std::vector& c_a = a; - for (std::size_t i = 0; i < 4; ++i) { + for (int i = 0; i < 4; ++i) { EXPECT_TRUE(&gsl::at(a, i) == &a[static_cast(i)]); EXPECT_TRUE(&gsl::at(c_a, i) == &a[static_cast(i)]); } @@ -81,7 +85,9 @@ TEST(at_tests, std_vector) std::abort(); }); + EXPECT_DEATH(gsl::at(a, -1), deathstring); EXPECT_DEATH(gsl::at(a, 4), deathstring); + EXPECT_DEATH(gsl::at(c_a, -1), deathstring); EXPECT_DEATH(gsl::at(c_a, 4), deathstring); } @@ -90,8 +96,8 @@ TEST(at_tests, InitializerList) const std::initializer_list a = {1, 2, 3, 4}; for (int i = 0; i < 4; ++i) { - EXPECT_TRUE(gsl::at(a, static_cast(i)) == i + 1); - EXPECT_TRUE(gsl::at({1, 2, 3, 4}, static_cast(i)) == i + 1); + EXPECT_TRUE(gsl::at(a, i) == i + 1); + EXPECT_TRUE(gsl::at({1, 2, 3, 4}, i) == i + 1); } std::set_terminate([] { @@ -99,7 +105,9 @@ TEST(at_tests, InitializerList) std::abort(); }); + EXPECT_DEATH(gsl::at(a, -1), deathstring); EXPECT_DEATH(gsl::at(a, 4), deathstring); + EXPECT_DEATH(gsl::at({1, 2, 3, 4}, -1), deathstring); EXPECT_DEATH(gsl::at({1, 2, 3, 4}, 4), deathstring); } @@ -112,12 +120,12 @@ static constexpr bool test_constexpr() const std::array& c_a2 = a2; for (int i = 0; i < 4; ++i) { - if (&gsl::at(a1, static_cast(i)) != &a1[i]) return false; - if (&gsl::at(c_a1, static_cast(i)) != &a1[i]) return false; + if (&gsl::at(a1, i) != &a1[i]) return false; + if (&gsl::at(c_a1, i) != &a1[i]) return false; // requires C++17: // if (&gsl::at(a2, i) != &a2[static_cast(i)]) return false; - if (&gsl::at(c_a2, static_cast(i)) != &c_a2[static_cast(i)]) return false; - if (gsl::at({1, 2, 3, 4}, static_cast(i)) != i + 1) return false; + if (&gsl::at(c_a2, i) != &c_a2[static_cast(i)]) return false; + if (gsl::at({1, 2, 3, 4}, i) != i + 1) return false; } return true; diff --git a/tests/utils_tests.cpp b/tests/utils_tests.cpp index 78bf59d..1fb0fd2 100644 --- a/tests/utils_tests.cpp +++ b/tests/utils_tests.cpp @@ -38,7 +38,7 @@ void g() { j += 1; } TEST(utils_tests, sanity_check_for_gsl_index_typedef) { - static_assert(std::is_same::value, + static_assert(std::is_same::value, "gsl::index represents wrong arithmetic type"); }