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); +} +