adding front and back

This commit is contained in:
Jordan Maples [MSFT] 2019-11-19 16:03:55 -08:00
parent 0fefba89da
commit 4b823b1651
2 changed files with 29 additions and 0 deletions

View File

@ -512,6 +512,19 @@ public:
constexpr reference at(index_type idx) const { return this->operator[](idx); } constexpr reference at(index_type idx) const { return this->operator[](idx); }
constexpr reference operator()(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(); } constexpr pointer data() const noexcept { return storage_.data(); }
// [span.iter], span iterator support // [span.iter], span iterator support

View File

@ -1644,6 +1644,22 @@ TEST(span_test, from_array_constructor)
EXPECT_FALSE((std::is_default_constructible<span<int, 42>>::value)); EXPECT_FALSE((std::is_default_constructible<span<int, 42>>::value));
} }
TEST(span_test, front_back)
{
int arr[5] = {1,2,3,4,5};
span<int> s{arr};
EXPECT_TRUE(s.front() == 1);
EXPECT_TRUE(s.back() == 5);
std::set_terminate([] {
std::cerr << "Expected Death. front_back";
std::abort();
});
span<int> s2;
EXPECT_DEATH(s2.front(), deathstring);
EXPECT_DEATH(s2.back(), deathstring);
}
#if __clang__ || __GNUC__ #if __clang__ || __GNUC__
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif // __clang__ || __GNUC__ #endif // __clang__ || __GNUC__