mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
gsl::at clean-up: (#479)
* initializer_list overload returns by value to avoid lifetime issues * generic overload uses expression SFINAE to work with any type that has member size() and operator[], which notably includes const/non-const vector and array. * Add test coverage for const objects, rvalue initializer_lists, and constexpr usage. Fixes #357.
This commit is contained in:
parent
d65660760b
commit
ebab8cab7f
@ -127,31 +127,25 @@ inline T narrow(U u)
|
|||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// at() - Bounds-checked way of accessing static arrays, std::array, std::vector
|
// at() - Bounds-checked way of accessing builtin arrays, std::array, std::vector
|
||||||
//
|
//
|
||||||
template <class T, std::size_t N>
|
template <class T, std::size_t N>
|
||||||
inline constexpr T& at(T (&arr)[N], std::ptrdiff_t index)
|
inline constexpr T& at(T (&arr)[N], const std::ptrdiff_t index)
|
||||||
{
|
|
||||||
Expects(index >= 0 && index < narrow_cast<std::ptrdiff_t>(N));
|
|
||||||
return arr[static_cast<std::size_t>(index)];
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class T, std::size_t N>
|
|
||||||
inline constexpr T& at(std::array<T, N>& arr, std::ptrdiff_t index)
|
|
||||||
{
|
{
|
||||||
Expects(index >= 0 && index < narrow_cast<std::ptrdiff_t>(N));
|
Expects(index >= 0 && index < narrow_cast<std::ptrdiff_t>(N));
|
||||||
return arr[static_cast<std::size_t>(index)];
|
return arr[static_cast<std::size_t>(index)];
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Cont>
|
template <class Cont>
|
||||||
inline constexpr typename Cont::value_type& at(Cont& cont, std::ptrdiff_t index)
|
inline constexpr auto at(Cont& cont, const std::ptrdiff_t index) -> decltype(cont[cont.size()])
|
||||||
{
|
{
|
||||||
Expects(index >= 0 && index < narrow_cast<std::ptrdiff_t>(cont.size()));
|
Expects(index >= 0 && index < narrow_cast<std::ptrdiff_t>(cont.size()));
|
||||||
return cont[static_cast<typename Cont::size_type>(index)];
|
using size_type = decltype(cont.size());
|
||||||
|
return cont[static_cast<size_type>(index)];
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
inline constexpr const T& at(std::initializer_list<T> cont, std::ptrdiff_t index)
|
inline constexpr T at(const std::initializer_list<T> cont, const std::ptrdiff_t index)
|
||||||
{
|
{
|
||||||
Expects(index >= 0 && index < narrow_cast<std::ptrdiff_t>(cont.size()));
|
Expects(index >= 0 && index < narrow_cast<std::ptrdiff_t>(cont.size()));
|
||||||
return *(cont.begin() + index);
|
return *(cont.begin() + index);
|
||||||
|
@ -19,57 +19,96 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
|
|
||||||
using namespace std;
|
using gsl::fail_fast;
|
||||||
using namespace gsl;
|
|
||||||
|
|
||||||
SUITE(at_tests)
|
SUITE(at_tests)
|
||||||
{
|
{
|
||||||
TEST(static_array)
|
TEST(static_array)
|
||||||
{
|
{
|
||||||
int a[] = { 1, 2, 3, 4 };
|
int a[4] = { 1, 2, 3, 4 };
|
||||||
|
const int (&c_a)[4] = a;
|
||||||
|
|
||||||
for (int i = 0; i < 4; ++i)
|
for (int i = 0; i < 4; ++i) {
|
||||||
CHECK(at(a, i) == i+1);
|
CHECK(&gsl::at(a, i) == &a[i]);
|
||||||
|
CHECK(&gsl::at(c_a, i) == &a[i]);
|
||||||
|
}
|
||||||
|
|
||||||
CHECK_THROW(at(a, -1), fail_fast);
|
CHECK_THROW(gsl::at(a, -1), fail_fast);
|
||||||
CHECK_THROW(at(a, 4), fail_fast);
|
CHECK_THROW(gsl::at(a, 4), fail_fast);
|
||||||
|
CHECK_THROW(gsl::at(c_a, -1), fail_fast);
|
||||||
|
CHECK_THROW(gsl::at(c_a, 4), fail_fast);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(std_array)
|
TEST(std_array)
|
||||||
{
|
{
|
||||||
std::array<int, 4> a = { 1, 2, 3, 4 };
|
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 (int i = 0; i < 4; ++i) {
|
||||||
CHECK(at(a, i) == i+1);
|
CHECK(&gsl::at(a, i) == &a[i]);
|
||||||
|
CHECK(&gsl::at(c_a, i) == &a[i]);
|
||||||
|
}
|
||||||
|
|
||||||
CHECK_THROW(at(a, -1), fail_fast);
|
CHECK_THROW(gsl::at(a, -1), fail_fast);
|
||||||
CHECK_THROW(at(a, 4), fail_fast);
|
CHECK_THROW(gsl::at(a, 4), fail_fast);
|
||||||
|
CHECK_THROW(gsl::at(c_a, -1), fail_fast);
|
||||||
|
CHECK_THROW(gsl::at(c_a, 4), fail_fast);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StdVector)
|
TEST(StdVector)
|
||||||
{
|
{
|
||||||
std::vector<int> a = { 1, 2, 3, 4 };
|
std::vector<int> a = { 1, 2, 3, 4 };
|
||||||
|
const std::vector<int>& c_a = a;
|
||||||
|
|
||||||
for (int i = 0; i < 4; ++i)
|
for (int i = 0; i < 4; ++i) {
|
||||||
CHECK(at(a, i) == i+1);
|
CHECK(&gsl::at(a, i) == &a[i]);
|
||||||
|
CHECK(&gsl::at(c_a, i) == &a[i]);
|
||||||
|
}
|
||||||
|
|
||||||
CHECK_THROW(at(a, -1), fail_fast);
|
CHECK_THROW(gsl::at(a, -1), fail_fast);
|
||||||
CHECK_THROW(at(a, 4), fail_fast);
|
CHECK_THROW(gsl::at(a, 4), fail_fast);
|
||||||
|
CHECK_THROW(gsl::at(c_a, -1), fail_fast);
|
||||||
|
CHECK_THROW(gsl::at(c_a, 4), fail_fast);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(InitializerList)
|
TEST(InitializerList)
|
||||||
{
|
{
|
||||||
std::initializer_list<int> a = { 1, 2, 3, 4 };
|
std::initializer_list<int> a = { 1, 2, 3, 4 };
|
||||||
|
|
||||||
for (int i = 0; i < 4; ++i)
|
for (int i = 0; i < 4; ++i) {
|
||||||
CHECK(at(a, i) == i+1);
|
CHECK(gsl::at(a, i) == i+1);
|
||||||
|
CHECK(gsl::at({1,2,3,4}, i) == i+1);
|
||||||
|
}
|
||||||
|
|
||||||
CHECK_THROW(at(a, -1), fail_fast);
|
CHECK_THROW(gsl::at(a, -1), fail_fast);
|
||||||
CHECK_THROW(at(a, 4), fail_fast);
|
CHECK_THROW(gsl::at(a, 4), fail_fast);
|
||||||
|
CHECK_THROW(gsl::at({1,2,3,4}, -1), fail_fast);
|
||||||
|
CHECK_THROW(gsl::at({1,2,3,4}, 4), fail_fast);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int, const char *[])
|
#if !defined(_MSC_VER) || (defined(__clang__) || _MSC_VER >= 1910)
|
||||||
|
static constexpr bool test_constexpr()
|
||||||
|
{
|
||||||
|
int a1[4] = { 1, 2, 3, 4 };
|
||||||
|
const int (&c_a1)[4] = a1;
|
||||||
|
std::array<int,4> a2 = { 1, 2, 3, 4 };
|
||||||
|
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(c_a2, i) != &c_a2[i]) return false;
|
||||||
|
if (gsl::at({1,2,3,4}, i) != i+1) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static_assert(test_constexpr(), "FAIL");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int main()
|
||||||
{
|
{
|
||||||
return UnitTest::RunAllTests();
|
return UnitTest::RunAllTests();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user