adding front and back

This commit is contained in:
Jordan Maples [MSFT] 2019-11-19 16:03:55 -08:00
parent 7e99e76c97
commit 7004ef506c
2 changed files with 27 additions and 1 deletions

View File

@ -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

View File

@ -1562,3 +1562,16 @@ TEST_CASE("default_constructible")
CHECK((!std::is_default_constructible<span<int, 42>>::value));
}
TEST_CASE("front_back")
{
int arr[5] = {1,2,3,4,5};
span<int> s{arr};
CHECK(s.front() == 1);
CHECK(s.back() == 5);
span<int> s2;
CHECK_THROWS_AS(s2.front(), fail_fast);
CHECK_THROWS_AS(s2.back(), fail_fast);
}