From b33bd4aebe99bf6688ec5af6261194044656a50f Mon Sep 17 00:00:00 2001 From: "Jordan Maples [MSFT]" <49793787+JordanMaples@users.noreply.github.com> Date: Wed, 8 Jan 2020 11:51:15 -0800 Subject: [PATCH 1/4] updating all span functions to be noexcept --- include/gsl/span | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/include/gsl/span b/include/gsl/span index 59bd121..d3ff3ca 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -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,14 +504,14 @@ 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 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 From b186b6cc680ffd6a4d18794674ee1eeadeb3af87 Mon Sep 17 00:00:00 2001 From: Martijn Otto Date: Thu, 14 Nov 2019 13:13:19 +0100 Subject: [PATCH 2/4] Implement free-standing (c|r|cr)(begin|end) and ssize functions --- include/gsl/span | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/include/gsl/span b/include/gsl/span index 59bd121..82e0fab 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -774,6 +774,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__) From fa8a8117a09a69e4cdfe4b86da0fe69d03080220 Mon Sep 17 00:00:00 2001 From: Martijn Otto Date: Fri, 15 Nov 2019 14:19:41 +0100 Subject: [PATCH 3/4] Add tests for free-standing iterator and size functions --- tests/span_tests.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index edbd118..7607194 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}; From 3bf4106a440f9e0b1363cfb3777274995f6733eb Mon Sep 17 00:00:00 2001 From: "Jordan Maples [MSFT]" <49793787+JordanMaples@users.noreply.github.com> Date: Wed, 8 Jan 2020 12:34:11 -0800 Subject: [PATCH 4/4] noexcept all of the span iterator stuff --- include/gsl/span | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/include/gsl/span b/include/gsl/span index d3ff3ca..6d0899c 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 {