From 134f2db5d9c9039b04cb2da1513c7ffdd7906fb8 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Tue, 23 Aug 2016 10:30:06 +0200 Subject: [PATCH] Specialize gsl::at for span span being a view and not a container, the generic version of gsl::at is not valid any more for span. This commits adds a specialization of gsl::at for span --- gsl/span | 8 ++++++++ tests/span_tests.cpp | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/gsl/span b/gsl/span index 1dc4600..7ba3f3d 100644 --- a/gsl/span +++ b/gsl/span @@ -653,6 +653,14 @@ as_writeable_bytes(span s) noexcept return {reinterpret_cast(s.data()), s.size_bytes()}; } +// Specialization of gsl::at for span +template +constexpr ElementType& at(const span& s, size_t index) +{ + // No bounds checking here because it is done in span::operator[] called below + return s[index]; +} + } // namespace gsl #ifdef _MSC_VER diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index 712492f..571a821 100644 --- a/tests/span_tests.cpp +++ b/tests/span_tests.cpp @@ -1354,6 +1354,13 @@ SUITE(span_tests) CHECK(match[0].first == f_it); CHECK(match[0].second == (f_it + 1)); } + + TEST(interop_with_gsl_at) + { + int arr[5] = {1, 2, 3, 4, 5}; + span s{arr}; + CHECK(at(s,0) == 1 && at(s,1) == 2); + } } int main(int, const char* []) { return UnitTest::RunAllTests(); }