gsl::index ptrdiff_t -> size_t. remove span::at span::operator().

This commit is contained in:
Jordan Maples [MSFT] 2020-02-04 16:53:43 -08:00
parent 5a1e4f3953
commit d7e1611137
5 changed files with 39 additions and 111 deletions

View File

@ -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 <class F>
@ -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<index>(N));
return arr[narrow_cast<std::size_t>(i)];
Expects(i >= 0 && i < N);
return arr[i];
}
template <class Cont>
@ -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<index>(cont.size()));
using size_type = decltype(cont.size());
return cont[narrow_cast<size_type>(i)];
Expects(i >= 0 && i < cont.size());
return cont[i];
}
template <class T>
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
constexpr T at(const std::initializer_list<T> cont, const index i)
{
Expects(i >= 0 && i < narrow_cast<index>(cont.size()));
Expects(i >= 0 && i < cont.size());
return *(cont.begin() + i);
}

View File

@ -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<ElementType, Extent> s, index i)
// [span.obs] Free observer functions
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::size_type
ssize(const span<ElementType, Extent>& span) noexcept
constexpr std::ptrdiff_t
ssize(const span<ElementType, Extent>& s) noexcept
{
return span.size();
return static_cast<std::ptrdiff_t>(s.size());
}
// [span.iter] Free functions for begin/end functions
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::iterator
begin(const span<ElementType, Extent>& span) noexcept
begin(const span<ElementType, Extent>& s) noexcept
{
return span.begin();
return s.begin();
}
template <class ElementType, std::size_t Extent = dynamic_extent>
constexpr typename span<ElementType, Extent>::iterator
end(const span<ElementType, Extent>& span) noexcept
end(const span<ElementType, Extent>& s) noexcept
{
return span.end();
return s.end();
}
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::const_iterator
cbegin(const span<ElementType, Extent>& span) noexcept
cbegin(const span<ElementType, Extent>& s) noexcept
{
return span.cbegin();
return s.cbegin();
}
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::const_iterator
cend(const span<ElementType, Extent>& span) noexcept
cend(const span<ElementType, Extent>& s) noexcept
{
return span.cend();
return s.cend();
}
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::reverse_iterator
rbegin(const span<ElementType, Extent>& span) noexcept
rbegin(const span<ElementType, Extent>& s) noexcept
{
return span.rbegin();
return s.rbegin();
}
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::reverse_iterator
rend(const span<ElementType, Extent>& span) noexcept
rend(const span<ElementType, Extent>& s) noexcept
{
return span.rend();
return s.rend();
}
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::const_reverse_iterator
crbegin(const span<ElementType, Extent>& span) noexcept
crbegin(const span<ElementType, Extent>& s) noexcept
{
return span.crbegin();
return s.crbegin();
}
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::const_reverse_iterator
crend(const span<ElementType, Extent>& span) noexcept
crend(const span<ElementType, Extent>& s) noexcept
{
return span.crend();
return s.crend();
}
} // namespace gsl

View File

@ -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<int, 4> a = {1, 2, 3, 4};
const std::array<int, 4>& 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<std::size_t>(i)]);
EXPECT_TRUE(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(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<int> a = {1, 2, 3, 4};
const std::vector<int>& 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<std::size_t>(i)]);
EXPECT_TRUE(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(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<int> 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<std::size_t>(i)) == i + 1);
EXPECT_TRUE(gsl::at({1, 2, 3, 4}, static_cast<std::size_t>(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<int, 4>& 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<std::size_t>(i)) != &a1[i]) return false;
if (&gsl::at(c_a1, static_cast<std::size_t>(i)) != &a1[i]) return false;
// requires C++17:
// if (&gsl::at(a2, i) != &a2[static_cast<std::size_t>(i)]) return false;
if (&gsl::at(c_a2, i) != &c_a2[static_cast<std::size_t>(i)]) return false;
if (gsl::at({1, 2, 3, 4}, i) != i + 1) return false;
if (&gsl::at(c_a2, static_cast<std::size_t>(i)) != &c_a2[static_cast<std::size_t>(i)]) return false;
if (gsl::at({1, 2, 3, 4}, static_cast<std::size_t>(i)) != i + 1) return false;
}
return true;

View File

@ -25,7 +25,7 @@
#include <iterator> // for reverse_iterator, operator-, operator==
#include <memory> // for unique_ptr, shared_ptr, make_unique, allo...
#include <regex> // for match_results, sub_match, match_results<>...
#include <stddef.h> // for ptrdiff_t
#include <cstddef> // for ptrdiff_t
#include <string> // for string
#include <type_traits> // for integral_constant<>::value, is_default_co...
#include <vector> // 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<int> 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<int> s = arr;
EXPECT_TRUE(s.at(0) == 1);
EXPECT_DEATH(s.at(5), deathstring);
}
{
int arr2d[2] = {1, 6};
span<int, 2> 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<int> s = arr;
EXPECT_TRUE(s(0) == 1);
EXPECT_DEATH(s(5), deathstring);
}
{
int arr2d[2] = {1, 6};
span<int, 2> s = arr2d;
EXPECT_TRUE(s(0) == 1);
EXPECT_TRUE(s(1) == 6);
EXPECT_DEATH(s(2), deathstring);
}
}
TEST(span_test, iterator_default_init)
{
span<int>::iterator it1;
@ -1093,8 +1039,8 @@ TEST(span_test, from_array_constructor)
int a[] = {1, 2, 3, 4};
span<int> s{a};
EXPECT_TRUE((std::is_same<decltype(s.size()), decltype(ssize(s))>::value));
EXPECT_TRUE(s.size() == ssize(s));
EXPECT_FALSE((std::is_same<decltype(s.size()), decltype(ssize(s))>::value));
EXPECT_TRUE(s.size() == static_cast<std::size_t>(ssize(s)));
}
TEST(span_test, iterator_comparisons)

View File

@ -23,6 +23,7 @@
#include <limits> // for numeric_limits
#include <stdint.h> // for uint32_t, int32_t
#include <type_traits> // for is_same
#include <cstddef> // 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<gsl::index, std::ptrdiff_t>::value,
static_assert(std::is_same<gsl::index, std::size_t>::value,
"gsl::index represents wrong arithmetic type");
}