From 7004ef506ce2a0deb60d52964c521ea69e9a39ed Mon Sep 17 00:00:00 2001 From: "Jordan Maples [MSFT]" <49793787+JordanMaples@users.noreply.github.com> Date: Tue, 19 Nov 2019 16:03:55 -0800 Subject: [PATCH 1/3] adding front and back --- include/gsl/span | 13 +++++++++++++ tests/span_tests.cpp | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/include/gsl/span b/include/gsl/span index 59bd121..c16ded4 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -512,6 +512,19 @@ public: constexpr reference at(index_type idx) const { return this->operator[](idx); } constexpr reference operator()(index_type idx) const { return this->operator[](idx); } + + constexpr reference front() const + { + Expects(size() > 0); + return data()[0]; + } + + constexpr reference back() const + { + Expects(size() > 0); + return data()[size() - 1]; + } + constexpr pointer data() const noexcept { return storage_.data(); } // [span.iter], span iterator support diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index d22dc08..f7d0026 100644 --- a/tests/span_tests.cpp +++ b/tests/span_tests.cpp @@ -893,7 +893,7 @@ TEST_CASE("subspan") { span av = arr; CHECK((av.subspan<1>().size() == 4)); - CHECK(decltype(av.subspan<1>())::extent == 4); + CHECK(decltype(av.subspan<1>())::extent == 4); } { @@ -1562,3 +1562,16 @@ TEST_CASE("default_constructible") CHECK((!std::is_default_constructible>::value)); } + +TEST_CASE("front_back") +{ + int arr[5] = {1,2,3,4,5}; + span s{arr}; + CHECK(s.front() == 1); + CHECK(s.back() == 5); + + span s2; + CHECK_THROWS_AS(s2.front(), fail_fast); + CHECK_THROWS_AS(s2.back(), fail_fast); +} + From 4b823b165177626de2e14b81c2acf0c92a87644f Mon Sep 17 00:00:00 2001 From: "Jordan Maples [MSFT]" <49793787+JordanMaples@users.noreply.github.com> Date: Tue, 19 Nov 2019 16:03:55 -0800 Subject: [PATCH 2/3] adding front and back --- include/gsl/span | 13 +++++++++++++ tests/span_tests.cpp | 16 ++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/gsl/span b/include/gsl/span index 59bd121..c16ded4 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -512,6 +512,19 @@ public: constexpr reference at(index_type idx) const { return this->operator[](idx); } constexpr reference operator()(index_type idx) const { return this->operator[](idx); } + + constexpr reference front() const + { + Expects(size() > 0); + return data()[0]; + } + + constexpr reference back() const + { + Expects(size() > 0); + return data()[size() - 1]; + } + constexpr pointer data() const noexcept { return storage_.data(); } // [span.iter], span iterator support diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index edbd118..ebeb685 100644 --- a/tests/span_tests.cpp +++ b/tests/span_tests.cpp @@ -1644,6 +1644,22 @@ TEST(span_test, from_array_constructor) EXPECT_FALSE((std::is_default_constructible>::value)); } + TEST(span_test, front_back) + { + int arr[5] = {1,2,3,4,5}; + span s{arr}; + EXPECT_TRUE(s.front() == 1); + EXPECT_TRUE(s.back() == 5); + + std::set_terminate([] { + std::cerr << "Expected Death. front_back"; + std::abort(); + }); + span s2; + EXPECT_DEATH(s2.front(), deathstring); + EXPECT_DEATH(s2.back(), deathstring); + } + #if __clang__ || __GNUC__ #pragma GCC diagnostic pop #endif // __clang__ || __GNUC__ From c7f9b3301ac10f464fff0a7d2d3f5e0eb4e24b97 Mon Sep 17 00:00:00 2001 From: "Jordan Maples [MSFT]" <49793787+JordanMaples@users.noreply.github.com> Date: Thu, 9 Jan 2020 12:13:12 -0800 Subject: [PATCH 3/3] noexcepting new functions --- include/gsl/span | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/gsl/span b/include/gsl/span index c16ded4..8536cf7 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -513,13 +513,13 @@ public: constexpr reference at(index_type idx) const { return this->operator[](idx); } constexpr reference operator()(index_type idx) const { return this->operator[](idx); } - constexpr reference front() const + constexpr reference front() const noexcept { Expects(size() > 0); return data()[0]; } - constexpr reference back() const + constexpr reference back() const noexcept { Expects(size() > 0); return data()[size() - 1];