diff --git a/include/gsl/span b/include/gsl/span index 8536cf7..6c1b2cd 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -164,67 +164,67 @@ namespace details return span_->data() + index_; } - constexpr span_iterator& operator++() + constexpr span_iterator& operator++() noexcept { Expects(0 <= index_ && index_ != span_->size()); ++index_; return *this; } - constexpr span_iterator operator++(int) + constexpr span_iterator operator++(int) noexcept { auto ret = *this; ++(*this); return ret; } - constexpr span_iterator& operator--() + constexpr span_iterator& operator--() noexcept { Expects(index_ != 0 && index_ <= span_->size()); --index_; return *this; } - constexpr span_iterator operator--(int) + constexpr span_iterator operator--(int) noexcept { auto ret = *this; --(*this); return ret; } - constexpr span_iterator operator+(difference_type n) const + constexpr span_iterator operator+(difference_type n) const noexcept { auto ret = *this; return ret += n; } - friend constexpr span_iterator operator+(difference_type n, span_iterator const& rhs) + friend constexpr span_iterator operator+(difference_type n, span_iterator const& rhs) noexcept { return rhs + n; } - constexpr span_iterator& operator+=(difference_type n) + constexpr span_iterator& operator+=(difference_type n) noexcept { Expects((index_ + n) >= 0 && (index_ + n) <= span_->size()); index_ += n; return *this; } - constexpr span_iterator operator-(difference_type n) const + constexpr span_iterator operator-(difference_type n) const noexcept { auto ret = *this; return ret -= n; } - constexpr span_iterator& operator-=(difference_type n) { return *this += -n; } + constexpr span_iterator& operator-=(difference_type n) noexcept { return *this += -n; } - constexpr difference_type operator-(span_iterator rhs) const + constexpr difference_type operator-(span_iterator rhs) const noexcept { Expects(span_ == rhs.span_); return index_ - rhs.index_; } - constexpr reference operator[](difference_type n) const { return *(*this + n); } + constexpr reference operator[](difference_type n) const noexcept { return *(*this + n); } constexpr friend bool operator==(span_iterator lhs, span_iterator rhs) noexcept { @@ -383,9 +383,9 @@ public: constexpr span() noexcept : storage_(nullptr, details::extent_type<0>()) {} - constexpr span(pointer ptr, index_type count) : storage_(ptr, count) {} + constexpr span(pointer ptr, index_type count) noexcept: storage_(ptr, count) {} - constexpr span(pointer firstElem, pointer lastElem) + constexpr span(pointer firstElem, pointer lastElem) noexcept : storage_(firstElem, std::distance(firstElem, lastElem)) {} @@ -424,7 +424,7 @@ public: std::is_convertible::value && std::is_convertible().data())>::value>> - constexpr span(Container& cont) : span(cont.data(), narrow(cont.size())) + constexpr span(Container& cont) noexcept : span(cont.data(), narrow(cont.size())) {} template ::value && std::is_convertible().data())>::value>> - constexpr span(const Container& cont) : span(cont.data(), narrow(cont.size())) + constexpr span(const Container& cont) noexcept : span(cont.data(), narrow(cont.size())) {} constexpr span(const span& other) noexcept = default; @@ -443,7 +443,7 @@ public: class = std::enable_if_t< details::is_allowed_extent_conversion::value && details::is_allowed_element_type_conversion::value>> - constexpr span(const span& other) + constexpr span(const span& other) noexcept : storage_(other.data(), details::extent_type(other.size())) {} @@ -452,7 +452,7 @@ public: // [span.sub], span subviews template - constexpr span first() const + constexpr span first() const noexcept { Expects(Count >= 0 && Count <= size()); return {data(), Count}; @@ -460,7 +460,7 @@ public: template GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute - constexpr span last() const + constexpr span last() const noexcept { Expects(Count >= 0 && size() - Count >= 0); return {data() + (size() - Count), Count}; @@ -468,7 +468,7 @@ public: template GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute - constexpr auto subspan() const -> + constexpr auto subspan() const noexcept -> typename details::calculate_subspan_type::type { Expects((Offset >= 0 && size() - Offset >= 0) && @@ -477,19 +477,19 @@ public: return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count}; } - constexpr span first(index_type count) const + constexpr span first(index_type count) const noexcept { Expects(count >= 0 && count <= size()); return {data(), count}; } - constexpr span last(index_type count) const + constexpr span last(index_type count) const noexcept { return make_subspan(size() - count, dynamic_extent, subspan_selector{}); } constexpr span subspan(index_type offset, - index_type count = dynamic_extent) const + index_type count = dynamic_extent) const noexcept { return make_subspan(offset, count, subspan_selector{}); } @@ -504,15 +504,12 @@ public: // [span.elem], span element access GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute - constexpr reference operator[](index_type idx) const + constexpr reference operator[](index_type idx) const noexcept { Expects(CheckRange(idx, storage_.size())); return data()[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 front() const noexcept { Expects(size() > 0); @@ -525,6 +522,10 @@ public: return data()[size() - 1]; } + + constexpr reference at(index_type idx) const noexcept{ return this->operator[](idx); } + constexpr reference operator()(index_type idx) const noexcept{ return this->operator[](idx); } + constexpr pointer data() const noexcept { return storage_.data(); } // [span.iter], span iterator support @@ -787,6 +788,62 @@ constexpr ElementType& at(span s, index i) return s[i]; } +// [span.obs] Free observer functions +template +constexpr typename span::index_type ssize(const span &span) noexcept +{ + return span.size(); +} + +// [span.iter] Free functions for begin/end functions +template +constexpr typename span::iterator begin(const span &span) noexcept +{ + return span.begin(); +} + +template +constexpr typename span::iterator end(const span &span) noexcept +{ + return span.end(); +} + +template +constexpr typename span::const_iterator cbegin(const span &span) noexcept +{ + return span.cbegin(); +} + +template +constexpr typename span::const_iterator cend(const span &span) noexcept +{ + return span.cend(); +} + +template +constexpr typename span::reverse_iterator rbegin(const span &span) noexcept +{ + return span.rbegin(); +} + +template +constexpr typename span::reverse_iterator rend(const span &span) noexcept +{ + return span.rend(); +} + +template +constexpr typename span::const_reverse_iterator crbegin(const span &span) noexcept +{ + return span.crbegin(); +} + +template +constexpr typename span::const_reverse_iterator crend(const span &span) noexcept +{ + return span.crend(); +} + } // namespace gsl #if defined(_MSC_VER) && !defined(__clang__) diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index ebeb685..5885328 100644 --- a/tests/span_tests.cpp +++ b/tests/span_tests.cpp @@ -1084,6 +1084,45 @@ TEST(span_test, from_array_constructor) EXPECT_TRUE(cit3 == s.cend()); } + TEST(span_test, iterator_free_functions) + { + int a[] = {1, 2, 3, 4}; + span s{a}; + + EXPECT_TRUE((std::is_same::value)); + EXPECT_TRUE((std::is_same::value)); + + EXPECT_TRUE((std::is_same::value)); + EXPECT_TRUE((std::is_same::value)); + + EXPECT_TRUE((std::is_same::value)); + EXPECT_TRUE((std::is_same::value)); + + EXPECT_TRUE((std::is_same::value)); + EXPECT_TRUE((std::is_same::value)); + + EXPECT_TRUE(s.begin() == begin(s)); + EXPECT_TRUE(s.end() == end(s)); + + EXPECT_TRUE(s.cbegin() == cbegin(s)); + EXPECT_TRUE(s.cend() == cend(s)); + + EXPECT_TRUE(s.rbegin() == rbegin(s)); + EXPECT_TRUE(s.rend() == rend(s)); + + EXPECT_TRUE(s.crbegin() == crbegin(s)); + EXPECT_TRUE(s.crend() == crend(s)); + } + + TEST(span_test, ssize_free_function) + { + int a[] = {1, 2, 3, 4}; + span s{a}; + + EXPECT_TRUE((std::is_same::value)); + EXPECT_TRUE(s.size() == ssize(s)); + } + TEST(span_test, iterator_comparisons) { int a[] = {1, 2, 3, 4};