diff --git a/include/gsl/gsl_util b/include/gsl/gsl_util index d1f7f33..53141e6 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::ptrdiff_t; +using index = std::size_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 < narrow_cast(N)); - return arr[narrow_cast(i)]; + Expects(i >= 0 && i < N); + return arr[i]; } template @@ -138,16 +138,15 @@ 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 < narrow_cast(cont.size())); - using size_type = decltype(cont.size()); - return cont[narrow_cast(i)]; + Expects(i >= 0 && i < cont.size()); + return cont[i]; } template GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute constexpr T at(const std::initializer_list cont, const index i) { - Expects(i >= 0 && i < narrow_cast(cont.size())); + Expects(i >= 0 && i < cont.size()); return *(cont.begin() + i); } diff --git a/include/gsl/span b/include/gsl/span index 5049b97..548587b 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -552,16 +552,6 @@ public: return data()[size() - 1]; } - // at and operator() are deprecated to align to the public member functions of std::span - [[deprecated("Use operator[]")]] constexpr reference at(size_type idx) const noexcept - { - return this->operator[](idx); - } - [[deprecated("Use operator[]")]] constexpr reference operator()(size_type idx) const noexcept - { - return this->operator[](idx); - } - constexpr pointer data() const noexcept { return storage_.data(); } // [span.iter], span iterator support @@ -817,67 +807,67 @@ constexpr ElementType& at(span s, index i) // [span.obs] Free observer functions template -constexpr typename span::size_type -ssize(const span& span) noexcept +constexpr std::ptrdiff_t +ssize(const span& s) noexcept { - return span.size(); + return static_cast(s.size()); } // [span.iter] Free functions for begin/end functions template constexpr typename span::iterator -begin(const span& span) noexcept +begin(const span& s) noexcept { - return span.begin(); + return s.begin(); } template constexpr typename span::iterator -end(const span& span) noexcept +end(const span& s) noexcept { - return span.end(); + return s.end(); } template constexpr typename span::const_iterator -cbegin(const span& span) noexcept +cbegin(const span& s) noexcept { - return span.cbegin(); + return s.cbegin(); } template constexpr typename span::const_iterator -cend(const span& span) noexcept +cend(const span& s) noexcept { - return span.cend(); + return s.cend(); } template constexpr typename span::reverse_iterator -rbegin(const span& span) noexcept +rbegin(const span& s) noexcept { - return span.rbegin(); + return s.rbegin(); } template constexpr typename span::reverse_iterator -rend(const span& span) noexcept +rend(const span& s) noexcept { - return span.rend(); + return s.rend(); } template constexpr typename span::const_reverse_iterator -crbegin(const span& span) noexcept +crbegin(const span& s) noexcept { - return span.crbegin(); + return s.crbegin(); } template constexpr typename span::const_reverse_iterator -crend(const span& span) noexcept +crend(const span& s) noexcept { - return span.crend(); + return s.crend(); } } // namespace gsl diff --git a/tests/at_tests.cpp b/tests/at_tests.cpp index be2c7b8..0580301 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 (int i = 0; i < 4; ++i) { + for (std::size_t i = 0; i < 4; ++i) { EXPECT_TRUE(&gsl::at(a, i) == &a[i]); EXPECT_TRUE(&gsl::at(c_a, i) == &a[i]); } @@ -43,9 +43,7 @@ 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); } @@ -54,7 +52,7 @@ TEST(at_tests, std_array) std::array a = {1, 2, 3, 4}; const std::array& c_a = a; - for (int i = 0; i < 4; ++i) { + for (std::size_t 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)]); } @@ -64,9 +62,7 @@ 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); } @@ -75,7 +71,7 @@ TEST(at_tests, std_vector) std::vector a = {1, 2, 3, 4}; const std::vector& c_a = a; - for (int i = 0; i < 4; ++i) { + for (std::size_t 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)]); } @@ -85,9 +81,7 @@ 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); } @@ -96,8 +90,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, i) == i + 1); - EXPECT_TRUE(gsl::at({1, 2, 3, 4}, i) == i + 1); + EXPECT_TRUE(gsl::at(a, static_cast(i)) == i + 1); + EXPECT_TRUE(gsl::at({1, 2, 3, 4}, static_cast(i)) == i + 1); } std::set_terminate([] { @@ -105,9 +99,7 @@ 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); } @@ -120,12 +112,12 @@ static constexpr bool test_constexpr() const std::array& c_a2 = a2; for (int i = 0; i < 4; ++i) { - if (&gsl::at(a1, i) != &a1[i]) return false; - if (&gsl::at(c_a1, i) != &a1[i]) return false; + if (&gsl::at(a1, static_cast(i)) != &a1[i]) return false; + if (&gsl::at(c_a1, static_cast(i)) != &a1[i]) return false; // requires C++17: // if (&gsl::at(a2, i) != &a2[static_cast(i)]) 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; + 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; } return true; diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index 99ee0d8..ce10325 100644 --- a/tests/span_tests.cpp +++ b/tests/span_tests.cpp @@ -25,7 +25,7 @@ #include // for reverse_iterator, operator-, operator== #include // for unique_ptr, shared_ptr, make_unique, allo... #include // for match_results, sub_match, match_results<>... -#include // for ptrdiff_t +#include // for ptrdiff_t #include // for string #include // for integral_constant<>::value, is_default_co... #include // for vector @@ -161,11 +161,7 @@ TEST(span_test, from_pointer_length_constructor) 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]); - EXPECT_TRUE(arr[j] == s.at(j)); - EXPECT_TRUE(arr[j] == s(j)); - } } { span s = {&arr[i], 4 - i}; @@ -174,11 +170,7 @@ TEST(span_test, from_pointer_length_constructor) EXPECT_TRUE(s.empty() == ((4 - i) == 0)); for (std::size_t j = 0; j < 4 - i; ++j) - { EXPECT_TRUE(arr[j + i] == s[j]); - EXPECT_TRUE(arr[j + i] == s.at(j)); - EXPECT_TRUE(arr[j + i] == s(j)); - } } } } @@ -976,52 +968,6 @@ TEST(span_test, from_array_constructor) } } - TEST(span_test, at_call) - { - std::set_terminate([] { - std::cerr << "Expected Death. at_call"; - std::abort(); - }); - int arr[4] = {1, 2, 3, 4}; - - { - span s = arr; - EXPECT_TRUE(s.at(0) == 1); - EXPECT_DEATH(s.at(5), deathstring); - } - - { - int arr2d[2] = {1, 6}; - span s = arr2d; - EXPECT_TRUE(s.at(0) == 1); - EXPECT_TRUE(s.at(1) == 6); - EXPECT_DEATH(s.at(2), deathstring); - } - } - - TEST(span_test, operator_function_call) - { - std::set_terminate([] { - std::cerr << "Expected Death. operator_function_call"; - std::abort(); - }); - int arr[4] = {1, 2, 3, 4}; - - { - span s = arr; - EXPECT_TRUE(s(0) == 1); - EXPECT_DEATH(s(5), deathstring); - } - - { - int arr2d[2] = {1, 6}; - span s = arr2d; - EXPECT_TRUE(s(0) == 1); - EXPECT_TRUE(s(1) == 6); - EXPECT_DEATH(s(2), deathstring); - } - } - TEST(span_test, iterator_default_init) { span::iterator it1; @@ -1093,8 +1039,8 @@ TEST(span_test, from_array_constructor) int a[] = {1, 2, 3, 4}; span s{a}; - EXPECT_TRUE((std::is_same::value)); - EXPECT_TRUE(s.size() == ssize(s)); + EXPECT_FALSE((std::is_same::value)); + EXPECT_TRUE(s.size() == static_cast(ssize(s))); } TEST(span_test, iterator_comparisons) diff --git a/tests/utils_tests.cpp b/tests/utils_tests.cpp index b6b5fc9..78bf59d 100644 --- a/tests/utils_tests.cpp +++ b/tests/utils_tests.cpp @@ -23,6 +23,7 @@ #include // for numeric_limits #include // for uint32_t, int32_t #include // for is_same +#include // for std::size_t using namespace gsl; @@ -37,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"); }