From 028925cababf51cb7ab6ed683c31d0fe3d3fd4de Mon Sep 17 00:00:00 2001 From: Neil MacIntosh Date: Sat, 10 Feb 2018 18:05:17 -0800 Subject: [PATCH 1/4] Removed from-smart-ptr constructors. --- include/gsl/span | 14 ------- tests/span_tests.cpp | 98 -------------------------------------------- 2 files changed, 112 deletions(-) diff --git a/include/gsl/span b/include/gsl/span index bd9a21d..fa76ed5 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -26,7 +26,6 @@ #include // for ptrdiff_t, size_t, nullptr_t #include // for reverse_iterator, distance, random_access_... #include -#include // for unique_ptr, shared_ptr #include #include // for enable_if_t, declval, is_convertible, inte... #include @@ -369,19 +368,6 @@ public: { } - template > - constexpr span(const std::unique_ptr& ptr, index_type count) - : storage_(ptr.get(), count) - { - } - - constexpr span(const std::unique_ptr& ptr) : storage_(ptr.get(), ptr.get() ? 1 : 0) - { - } - constexpr span(const std::shared_ptr& ptr) : storage_(ptr.get(), ptr.get() ? 1 : 0) - { - } - // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement // on Container to be a contiguous sequence container. template (4); - - { - span s{ptr}; - CHECK((s.length() == 1 && s.data() == ptr.get())); - CHECK(s[0] == 4); - } - - { - auto s = make_span(ptr); - CHECK((s.length() == 1 && s.data() == ptr.get())); - CHECK(s[0] == 4); - } - } - - { - auto ptr = std::unique_ptr{nullptr}; - - { - span s{ptr}; - CHECK((s.length() == 0 && s.data() == nullptr)); - } - - { - auto s = make_span(ptr); - CHECK((s.length() == 0 && s.data() == nullptr)); - } - } - - { - auto arr = std::make_unique(4); - - for (auto i = 0U; i < 4; i++) arr[i] = gsl::narrow_cast(i + 1); - - { - span s{arr, 4}; - CHECK((s.length() == 4 && s.data() == arr.get())); - CHECK((s[0] == 1 && s[1] == 2)); - } - - { - auto s = make_span(arr, 4); - CHECK((s.length() == 4 && s.data() == arr.get())); - CHECK((s[0] == 1 && s[1] == 2)); - } - } - - { - auto arr = std::unique_ptr{nullptr}; - - { - span s{arr, 0}; - CHECK((s.length() == 0 && s.data() == nullptr)); - } - - { - auto s = make_span(arr, 0); - CHECK((s.length() == 0 && s.data() == nullptr)); - } - } -} - -TEST_CASE("from_shared_pointer_construction") -{ - { - auto ptr = std::make_shared(4); - - { - span s{ptr}; - CHECK((s.length() == 1 && s.data() == ptr.get())); - CHECK((s[0] == 4)); - } - - { - auto s = make_span(ptr); - CHECK((s.length() == 1 && s.data() == ptr.get())); - CHECK((s[0] == 4)); - } - } - - { - auto ptr = std::shared_ptr{nullptr}; - - { - span s{ptr}; - CHECK((s.length() == 0 && s.data() == nullptr)); - } - - { - auto s = make_span(ptr); - CHECK((s.length() == 0 && s.data() == nullptr)); - } - } -} - TEST_CASE("from_container_constructor") { std::vector v = {1, 2, 3}; From 9ed6ecc4e0c3da31bf9060f6f61ea829c282a2ae Mon Sep 17 00:00:00 2001 From: Neil MacIntosh Date: Sat, 10 Feb 2018 18:25:07 -0800 Subject: [PATCH 2/4] Made all span iterator access functions constexpr. --- include/gsl/span | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/include/gsl/span b/include/gsl/span index fa76ed5..1c8b2e5 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -482,17 +482,17 @@ public: constexpr pointer data() const GSL_NOEXCEPT { return storage_.data(); } // [span.iter], span iterator support - iterator begin() const GSL_NOEXCEPT { return {this, 0}; } - iterator end() const GSL_NOEXCEPT { return {this, length()}; } + constexpr iterator begin() const GSL_NOEXCEPT { return {this, 0}; } + constexpr iterator end() const GSL_NOEXCEPT { return {this, length()}; } - const_iterator cbegin() const GSL_NOEXCEPT { return {this, 0}; } - const_iterator cend() const GSL_NOEXCEPT { return {this, length()}; } + constexpr const_iterator cbegin() const GSL_NOEXCEPT { return {this, 0}; } + constexpr const_iterator cend() const GSL_NOEXCEPT { return {this, length()}; } - reverse_iterator rbegin() const GSL_NOEXCEPT { return reverse_iterator{end()}; } - reverse_iterator rend() const GSL_NOEXCEPT { return reverse_iterator{begin()}; } + constexpr reverse_iterator rbegin() const GSL_NOEXCEPT { return reverse_iterator{end()}; } + constexpr reverse_iterator rend() const GSL_NOEXCEPT { return reverse_iterator{begin()}; } - const_reverse_iterator crbegin() const GSL_NOEXCEPT { return const_reverse_iterator{cend()}; } - const_reverse_iterator crend() const GSL_NOEXCEPT { return const_reverse_iterator{cbegin()}; } + constexpr const_reverse_iterator crbegin() const GSL_NOEXCEPT { return const_reverse_iterator{cend()}; } + constexpr const_reverse_iterator crend() const GSL_NOEXCEPT { return const_reverse_iterator{cbegin()}; } private: // this implementation detail class lets us take advantage of the From 72688ff009b7fa96099c6c3ea685b40085b5ef79 Mon Sep 17 00:00:00 2001 From: Neil MacIntosh Date: Sat, 10 Feb 2018 18:58:28 -0800 Subject: [PATCH 3/4] Removed span.length() as synonym for span.size(). --- include/gsl/span | 18 ++-- include/gsl/string_span | 2 +- tests/span_tests.cpp | 234 ++++++++++++++++++++-------------------- 3 files changed, 126 insertions(+), 128 deletions(-) diff --git a/include/gsl/span b/include/gsl/span index 1c8b2e5..2f076e0 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -128,7 +128,7 @@ namespace details constexpr span_iterator(const Span* span, typename Span::index_type index) GSL_NOEXCEPT : span_(span), index_(index) { - Expects(span == nullptr || (0 <= index_ && index <= span_->length())); + Expects(span == nullptr || (0 <= index_ && index <= span_->size())); } friend span_iterator; @@ -140,19 +140,19 @@ namespace details constexpr reference operator*() const GSL_NOEXCEPT { - Expects(index_ != span_->length()); + Expects(index_ != span_->size()); return *(span_->data() + index_); } constexpr pointer operator->() const GSL_NOEXCEPT { - Expects(index_ != span_->length()); + Expects(index_ != span_->size()); return span_->data() + index_; } constexpr span_iterator& operator++() GSL_NOEXCEPT { - Expects(0 <= index_ && index_ != span_->length()); + Expects(0 <= index_ && index_ != span_->size()); ++index_; return *this; } @@ -166,7 +166,7 @@ namespace details constexpr span_iterator& operator--() GSL_NOEXCEPT { - Expects(index_ != 0 && index_ <= span_->length()); + Expects(index_ != 0 && index_ <= span_->size()); --index_; return *this; } @@ -186,7 +186,7 @@ namespace details constexpr span_iterator& operator+=(difference_type n) GSL_NOEXCEPT { - Expects((index_ + n) >= 0 && (index_ + n) <= span_->length()); + Expects((index_ + n) >= 0 && (index_ + n) <= span_->size()); index_ += n; return *this; } @@ -461,9 +461,7 @@ public: // [span.obs], span observers - constexpr index_type length() const GSL_NOEXCEPT { return size(); } constexpr index_type size() const GSL_NOEXCEPT { return storage_.size(); } - constexpr index_type length_bytes() const GSL_NOEXCEPT { return size_bytes(); } constexpr index_type size_bytes() const GSL_NOEXCEPT { return size() * narrow_cast(sizeof(element_type)); @@ -483,10 +481,10 @@ public: // [span.iter], span iterator support constexpr iterator begin() const GSL_NOEXCEPT { return {this, 0}; } - constexpr iterator end() const GSL_NOEXCEPT { return {this, length()}; } + constexpr iterator end() const GSL_NOEXCEPT { return {this, size()}; } constexpr const_iterator cbegin() const GSL_NOEXCEPT { return {this, 0}; } - constexpr const_iterator cend() const GSL_NOEXCEPT { return {this, length()}; } + constexpr const_iterator cend() const GSL_NOEXCEPT { return {this, size()}; } constexpr reverse_iterator rbegin() const GSL_NOEXCEPT { return reverse_iterator{end()}; } constexpr reverse_iterator rend() const GSL_NOEXCEPT { return reverse_iterator{begin()}; } diff --git a/include/gsl/string_span b/include/gsl/string_span index fa8b515..c56d136 100644 --- a/include/gsl/string_span +++ b/include/gsl/string_span @@ -145,7 +145,7 @@ template span::type, dynamic_extent> ensure_z(Cont& cont) { - return ensure_z(cont.data(), static_cast(cont.length())); + return ensure_z(cont.data(), static_cast(cont.size())); } template diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index 3e487cd..ebbf5ef 100644 --- a/tests/span_tests.cpp +++ b/tests/span_tests.cpp @@ -51,33 +51,33 @@ TEST_CASE("default_constructor") { { span s; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } { span s; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } { #ifdef CONFIRM_COMPILATION_ERRORS span s; - CHECK((s.length() == 1 && s.data() == nullptr)); // explains why it can't compile + CHECK((s.size() == 1 && s.data() == nullptr)); // explains why it can't compile #endif } { span s{}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs{}; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } } @@ -98,60 +98,60 @@ TEST_CASE("from_nullptr_constructor") { { span s = nullptr; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs = nullptr; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } { span s = nullptr; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs = nullptr; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } { #ifdef CONFIRM_COMPILATION_ERRORS span s = nullptr; - CHECK((s.length() == 1 && s.data() == nullptr)); // explains why it can't compile + CHECK((s.size() == 1 && s.data() == nullptr)); // explains why it can't compile #endif } { span s{nullptr}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs{nullptr}; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } { span s{nullptr}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs{nullptr}; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } } -TEST_CASE("from_nullptr_length_constructor") +TEST_CASE("from_nullptr_size_constructor") { { span s{nullptr, static_cast::index_type>(0)}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs{nullptr, static_cast::index_type>(0)}; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } { span s{nullptr, static_cast::index_type>(0)}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs{nullptr, static_cast::index_type>(0)}; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } { @@ -179,10 +179,10 @@ TEST_CASE("from_nullptr_length_constructor") { span s{nullptr, static_cast::index_type>(0)}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); span cs{nullptr, static_cast::index_type>(0)}; - CHECK((cs.length() == 0 && cs.data() == nullptr)); + CHECK((cs.size() == 0 && cs.data() == nullptr)); } } @@ -195,7 +195,7 @@ TEST_CASE("from_pointer_length_constructor") { { span s = { &arr[0], i }; - CHECK(s.length() == i); + CHECK(s.size() == i); CHECK(s.data() == &arr[0]); CHECK(s.empty() == (i == 0)); for (int j = 0; j < i; ++j) @@ -207,7 +207,7 @@ TEST_CASE("from_pointer_length_constructor") } { span s = { &arr[i], 4-i }; - CHECK(s.length() == 4-i); + CHECK(s.size() == 4-i); CHECK(s.data() == &arr[i]); CHECK(s.empty() == (4-i == 0)); for (int j = 0; j < 4-i; ++j) @@ -222,14 +222,14 @@ TEST_CASE("from_pointer_length_constructor") { span s{&arr[0], 2}; - CHECK((s.length() == 2 && s.data() == &arr[0])); + CHECK((s.size() == 2 && s.data() == &arr[0])); CHECK((s[0] == 1 && s[1] == 2)); } { int* p = nullptr; span s{p, static_cast::index_type>(0)}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); } { @@ -240,14 +240,14 @@ TEST_CASE("from_pointer_length_constructor") { auto s = make_span(&arr[0], 2); - CHECK((s.length() == 2 && s.data() == &arr[0])); + CHECK((s.size() == 2 && s.data() == &arr[0])); CHECK((s[0] == 1 && s[1] == 2)); } { int* p = nullptr; auto s = make_span(p, static_cast::index_type>(0)); - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); } { @@ -263,24 +263,24 @@ TEST_CASE("from_pointer_pointer_constructor") { span s{&arr[0], &arr[2]}; - CHECK((s.length() == 2 && s.data() == &arr[0])); + CHECK((s.size() == 2 && s.data() == &arr[0])); CHECK((s[0] == 1 && s[1] == 2)); } { span s{&arr[0], &arr[2]}; - CHECK((s.length() == 2 && s.data() == &arr[0])); + CHECK((s.size() == 2 && s.data() == &arr[0])); CHECK((s[0] == 1 && s[1] == 2)); } { span s{&arr[0], &arr[0]}; - CHECK((s.length() == 0 && s.data() == &arr[0])); + CHECK((s.size() == 0 && s.data() == &arr[0])); } { span s{&arr[0], &arr[0]}; - CHECK((s.length() == 0 && s.data() == &arr[0])); + CHECK((s.size() == 0 && s.data() == &arr[0])); } // this will fail the std::distance() precondition, which asserts on MSVC debug builds @@ -299,13 +299,13 @@ TEST_CASE("from_pointer_pointer_constructor") { int* p = nullptr; span s{p, p}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); } { int* p = nullptr; span s{p, p}; - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); } // this will fail the std::distance() precondition, which asserts on MSVC debug builds @@ -317,19 +317,19 @@ TEST_CASE("from_pointer_pointer_constructor") { auto s = make_span(&arr[0], &arr[2]); - CHECK((s.length() == 2 && s.data() == &arr[0])); + CHECK((s.size() == 2 && s.data() == &arr[0])); CHECK((s[0] == 1 && s[1] == 2)); } { auto s = make_span(&arr[0], &arr[0]); - CHECK((s.length() == 0 && s.data() == &arr[0])); + CHECK((s.size() == 0 && s.data() == &arr[0])); } { int* p = nullptr; auto s = make_span(p, p); - CHECK((s.length() == 0 && s.data() == nullptr)); + CHECK((s.size() == 0 && s.data() == nullptr)); } } @@ -339,12 +339,12 @@ TEST_CASE("from_array_constructor") { span s{arr}; - CHECK((s.length() == 5 && s.data() == &arr[0])); + CHECK((s.size() == 5 && s.data() == &arr[0])); } { span s{arr}; - CHECK((s.length() == 5 && s.data() == &arr[0])); + CHECK((s.size() == 5 && s.data() == &arr[0])); } int arr2d[2][3] = {1, 2, 3, 4, 5, 6}; @@ -356,18 +356,18 @@ TEST_CASE("from_array_constructor") { span s{arr}; - CHECK((s.length() == 0 && s.data() == &arr[0])); + CHECK((s.size() == 0 && s.data() == &arr[0])); } { span s{arr2d}; - CHECK((s.length() == 6 && s.data() == &arr2d[0][0])); + CHECK((s.size() == 6 && s.data() == &arr2d[0][0])); CHECK((s[0] == 1 && s[5] == 6)); } { span s{arr2d}; - CHECK((s.length() == 0 && s.data() == &arr2d[0][0])); + CHECK((s.size() == 0 && s.data() == &arr2d[0][0])); } { @@ -376,7 +376,7 @@ TEST_CASE("from_array_constructor") #endif { span s{&(arr2d[0]), 1}; - CHECK((s.length() == 1 && s.data() == &arr2d[0])); + CHECK((s.size() == 1 && s.data() == &arr2d[0])); } int arr3d[2][3][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; @@ -384,13 +384,13 @@ TEST_CASE("from_array_constructor") #ifdef CONFIRM_COMPILATION_ERRORS { span s{arr3d}; - CHECK((s.length() == 12 && s.data() == &arr3d[0][0][0])); + CHECK((s.size() == 12 && s.data() == &arr3d[0][0][0])); CHECK((s[0] == 1 && s[11] == 12)); } { span s{arr3d}; - CHECK((s.length() == 0 && s.data() == &arr3d[0][0][0])); + CHECK((s.size() == 0 && s.data() == &arr3d[0][0][0])); } { @@ -399,28 +399,28 @@ TEST_CASE("from_array_constructor") { span s{arr3d}; - CHECK((s.length() == 12 && s.data() == &arr3d[0][0][0])); + CHECK((s.size() == 12 && s.data() == &arr3d[0][0][0])); CHECK((s[0] == 1 && s[5] == 6)); } #endif { span s{&arr3d[0], 1}; - CHECK((s.length() == 1 && s.data() == &arr3d[0])); + CHECK((s.size() == 1 && s.data() == &arr3d[0])); } { auto s = make_span(arr); - CHECK((s.length() == 5 && s.data() == &arr[0])); + CHECK((s.size() == 5 && s.data() == &arr[0])); } { auto s = make_span(&(arr2d[0]), 1); - CHECK((s.length() == 1 && s.data() == &arr2d[0])); + CHECK((s.size() == 1 && s.data() == &arr2d[0])); } { auto s = make_span(&arr3d[0], 1); - CHECK((s.length() == 1 && s.data() == &arr3d[0])); + CHECK((s.size() == 1 && s.data() == &arr3d[0])); } } @@ -430,12 +430,12 @@ TEST_CASE("from_dynamic_array_constructor") { span s(&arr[0][0][0], 10); - CHECK((s.length() == 10 && s.data() == &arr[0][0][0])); + CHECK((s.size() == 10 && s.data() == &arr[0][0][0])); } { auto s = make_span(&arr[0][0][0], 10); - CHECK((s.length() == 10 && s.data() == &arr[0][0][0])); + CHECK((s.size() == 10 && s.data() == &arr[0][0][0])); } delete[] arr; @@ -723,17 +723,17 @@ TEST_CASE("copy_move_and_assignment") int arr[] = {3, 4, 5}; span s2 = arr; - CHECK((s2.length() == 3 && s2.data() == &arr[0])); + CHECK((s2.size() == 3 && s2.data() == &arr[0])); s2 = s1; CHECK(s2.empty()); auto get_temp_span = [&]() -> span { return {&arr[1], 2}; }; - auto use_span = [&](span s) { CHECK((s.length() == 2 && s.data() == &arr[1])); }; + auto use_span = [&](span s) { CHECK((s.size() == 2 && s.data() == &arr[1])); }; use_span(get_temp_span()); s1 = get_temp_span(); - CHECK((s1.length() == 2 && s1.data() == &arr[1])); + CHECK((s1.size() == 2 && s1.data() == &arr[1])); } TEST_CASE("first") @@ -742,35 +742,35 @@ TEST_CASE("first") { span av = arr; - CHECK(av.first<2>().length() == 2); - CHECK(av.first(2).length() == 2); + CHECK(av.first<2>().size() == 2); + CHECK(av.first(2).size() == 2); } { span av = arr; - CHECK(av.first<0>().length() == 0); - CHECK(av.first(0).length() == 0); + CHECK(av.first<0>().size() == 0); + CHECK(av.first(0).size() == 0); } { span av = arr; - CHECK(av.first<5>().length() == 5); - CHECK(av.first(5).length() == 5); + CHECK(av.first<5>().size() == 5); + CHECK(av.first(5).size() == 5); } { span av = arr; #ifdef CONFIRM_COMPILATION_ERRORS - CHECK(av.first<6>().length() == 6); - CHECK(av.first<-1>().length() == -1); + CHECK(av.first<6>().size() == 6); + CHECK(av.first<-1>().size() == -1); #endif - CHECK_THROWS_AS(av.first(6).length(), fail_fast); + CHECK_THROWS_AS(av.first(6).size(), fail_fast); } { span av; - CHECK(av.first<0>().length() == 0); - CHECK(av.first(0).length() == 0); + CHECK(av.first<0>().size() == 0); + CHECK(av.first(0).size() == 0); } } @@ -780,34 +780,34 @@ TEST_CASE("last") { span av = arr; - CHECK(av.last<2>().length() == 2); - CHECK(av.last(2).length() == 2); + CHECK(av.last<2>().size() == 2); + CHECK(av.last(2).size() == 2); } { span av = arr; - CHECK(av.last<0>().length() == 0); - CHECK(av.last(0).length() == 0); + CHECK(av.last<0>().size() == 0); + CHECK(av.last(0).size() == 0); } { span av = arr; - CHECK(av.last<5>().length() == 5); - CHECK(av.last(5).length() == 5); + CHECK(av.last<5>().size() == 5); + CHECK(av.last(5).size() == 5); } { span av = arr; #ifdef CONFIRM_COMPILATION_ERRORS - CHECK(av.last<6>().length() == 6); + CHECK(av.last<6>().size() == 6); #endif - CHECK_THROWS_AS(av.last(6).length(), fail_fast); + CHECK_THROWS_AS(av.last(6).size(), fail_fast); } { span av; - CHECK(av.last<0>().length() == 0); - CHECK(av.last(0).length() == 0); + CHECK(av.last<0>().size() == 0); + CHECK(av.last(0).size() == 0); } } @@ -817,64 +817,64 @@ TEST_CASE("subspan") { span av = arr; - CHECK((av.subspan<2, 2>().length() == 2)); - CHECK(av.subspan(2, 2).length() == 2); - CHECK(av.subspan(2, 3).length() == 3); + CHECK((av.subspan<2, 2>().size() == 2)); + CHECK(av.subspan(2, 2).size() == 2); + CHECK(av.subspan(2, 3).size() == 3); } { span av = arr; - CHECK((av.subspan<0, 0>().length() == 0)); - CHECK(av.subspan(0, 0).length() == 0); + CHECK((av.subspan<0, 0>().size() == 0)); + CHECK(av.subspan(0, 0).size() == 0); } { span av = arr; - CHECK((av.subspan<0, 5>().length() == 5)); - CHECK(av.subspan(0, 5).length() == 5); - CHECK_THROWS_AS(av.subspan(0, 6).length(), fail_fast); - CHECK_THROWS_AS(av.subspan(1, 5).length(), fail_fast); + CHECK((av.subspan<0, 5>().size() == 5)); + CHECK(av.subspan(0, 5).size() == 5); + CHECK_THROWS_AS(av.subspan(0, 6).size(), fail_fast); + CHECK_THROWS_AS(av.subspan(1, 5).size(), fail_fast); } { span av = arr; - CHECK((av.subspan<4, 0>().length() == 0)); - CHECK(av.subspan(4, 0).length() == 0); - CHECK(av.subspan(5, 0).length() == 0); - CHECK_THROWS_AS(av.subspan(6, 0).length(), fail_fast); + CHECK((av.subspan<4, 0>().size() == 0)); + CHECK(av.subspan(4, 0).size() == 0); + CHECK(av.subspan(5, 0).size() == 0); + CHECK_THROWS_AS(av.subspan(6, 0).size(), fail_fast); } { span av; - CHECK((av.subspan<0, 0>().length() == 0)); - CHECK(av.subspan(0, 0).length() == 0); - CHECK_THROWS_AS((av.subspan<1, 0>().length()), fail_fast); + CHECK((av.subspan<0, 0>().size() == 0)); + CHECK(av.subspan(0, 0).size() == 0); + CHECK_THROWS_AS((av.subspan<1, 0>().size()), fail_fast); } { span av; - CHECK(av.subspan(0).length() == 0); - CHECK_THROWS_AS(av.subspan(1).length(), fail_fast); + CHECK(av.subspan(0).size() == 0); + CHECK_THROWS_AS(av.subspan(1).size(), fail_fast); } { span av = arr; - CHECK(av.subspan(0).length() == 5); - CHECK(av.subspan(1).length() == 4); - CHECK(av.subspan(4).length() == 1); - CHECK(av.subspan(5).length() == 0); - CHECK_THROWS_AS(av.subspan(6).length(), fail_fast); + CHECK(av.subspan(0).size() == 5); + CHECK(av.subspan(1).size() == 4); + CHECK(av.subspan(4).size() == 1); + CHECK(av.subspan(5).size() == 0); + CHECK_THROWS_AS(av.subspan(6).size(), fail_fast); const auto av2 = av.subspan(1); for (int i = 0; i < 4; ++i) CHECK(av2[i] == i + 2); } { span av = arr; - CHECK(av.subspan(0).length() == 5); - CHECK(av.subspan(1).length() == 4); - CHECK(av.subspan(4).length() == 1); - CHECK(av.subspan(5).length() == 0); - CHECK_THROWS_AS(av.subspan(6).length(), fail_fast); + CHECK(av.subspan(0).size() == 5); + CHECK(av.subspan(1).size() == 4); + CHECK(av.subspan(4).size() == 1); + CHECK(av.subspan(5).size() == 0); + CHECK_THROWS_AS(av.subspan(6).size(), fail_fast); const auto av2 = av.subspan(1); for (int i = 0; i < 4; ++i) CHECK(av2[i] == i + 2); } @@ -1315,17 +1315,17 @@ TEST_CASE("as_bytes") { const span s = a; - CHECK(s.length() == 4); + CHECK(s.size() == 4); const span bs = as_bytes(s); CHECK(static_cast(bs.data()) == static_cast(s.data())); - CHECK(bs.length() == s.length_bytes()); + CHECK(bs.size() == s.size_bytes()); } { span s; const auto bs = as_bytes(s); - CHECK(bs.length() == s.length()); - CHECK(bs.length() == 0); + CHECK(bs.size() == s.size()); + CHECK(bs.size() == 0); CHECK(bs.size_bytes() == 0); CHECK(static_cast(bs.data()) == static_cast(s.data())); CHECK(bs.data() == nullptr); @@ -1335,7 +1335,7 @@ TEST_CASE("as_bytes") span s = a; const auto bs = as_bytes(s); CHECK(static_cast(bs.data()) == static_cast(s.data())); - CHECK(bs.length() == s.length_bytes()); + CHECK(bs.size() == s.size_bytes()); } } @@ -1347,18 +1347,18 @@ TEST_CASE("as_writeable_bytes") #ifdef CONFIRM_COMPILATION_ERRORS // you should not be able to get writeable bytes for const objects span s = a; - CHECK(s.length() == 4); + CHECK(s.size() == 4); span bs = as_writeable_bytes(s); CHECK(static_cast(bs.data()) == static_cast(s.data())); - CHECK(bs.length() == s.length_bytes()); + CHECK(bs.size() == s.size_bytes()); #endif } { span s; const auto bs = as_writeable_bytes(s); - CHECK(bs.length() == s.length()); - CHECK(bs.length() == 0); + CHECK(bs.size() == s.size()); + CHECK(bs.size() == 0); CHECK(bs.size_bytes() == 0); CHECK(static_cast(bs.data()) == static_cast(s.data())); CHECK(bs.data() == nullptr); @@ -1368,7 +1368,7 @@ TEST_CASE("as_writeable_bytes") span s = a; const auto bs = as_writeable_bytes(s); CHECK(static_cast(bs.data()) == static_cast(s.data())); - CHECK(bs.length() == s.length_bytes()); + CHECK(bs.size() == s.size_bytes()); } } @@ -1378,12 +1378,12 @@ TEST_CASE("fixed_size_conversions") // converting to an span from an equal size array is ok span s4 = arr; - CHECK(s4.length() == 4); + CHECK(s4.size() == 4); // converting to dynamic_range is always ok { span s = s4; - CHECK(s.length() == s4.length()); + CHECK(s.size() == s4.size()); static_cast(s); } From 2bdbba7418e5b06d100e77831135f8f45970a6cd Mon Sep 17 00:00:00 2001 From: Neil MacIntosh Date: Sat, 10 Feb 2018 19:21:22 -0800 Subject: [PATCH 4/4] Removed span from-nullptr_t ctor. --- include/gsl/span | 2 -- include/gsl/string_span | 3 --- tests/span_tests.cpp | 48 +++---------------------------------- tests/string_span_tests.cpp | 6 ----- 4 files changed, 3 insertions(+), 56 deletions(-) diff --git a/include/gsl/span b/include/gsl/span index 2f076e0..51b46f2 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -341,8 +341,6 @@ public: { } - constexpr span(std::nullptr_t) GSL_NOEXCEPT : span() {} - constexpr span(pointer ptr, index_type count) : storage_(ptr, count) {} constexpr span(pointer firstElem, pointer lastElem) diff --git a/include/gsl/string_span b/include/gsl/string_span index c56d136..42f8975 100644 --- a/include/gsl/string_span +++ b/include/gsl/string_span @@ -203,9 +203,6 @@ public: // move assign constexpr basic_string_span& operator=(basic_string_span&& other) GSL_NOEXCEPT = default; - // from nullptr - constexpr basic_string_span(std::nullptr_t ptr) GSL_NOEXCEPT : span_(ptr) {} - constexpr basic_string_span(pointer ptr, index_type length) : span_(ptr, length) {} constexpr basic_string_span(pointer firstElem, pointer lastElem) : span_(firstElem, lastElem) {} diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index ebbf5ef..a71a228 100644 --- a/tests/span_tests.cpp +++ b/tests/span_tests.cpp @@ -94,48 +94,6 @@ TEST_CASE("size_optimization") } } -TEST_CASE("from_nullptr_constructor") -{ - { - span s = nullptr; - CHECK((s.size() == 0 && s.data() == nullptr)); - - span cs = nullptr; - CHECK((cs.size() == 0 && cs.data() == nullptr)); - } - - { - span s = nullptr; - CHECK((s.size() == 0 && s.data() == nullptr)); - - span cs = nullptr; - CHECK((cs.size() == 0 && cs.data() == nullptr)); - } - - { -#ifdef CONFIRM_COMPILATION_ERRORS - span s = nullptr; - CHECK((s.size() == 1 && s.data() == nullptr)); // explains why it can't compile -#endif - } - - { - span s{nullptr}; - CHECK((s.size() == 0 && s.data() == nullptr)); - - span cs{nullptr}; - CHECK((cs.size() == 0 && cs.data() == nullptr)); - } - - { - span s{nullptr}; - CHECK((s.size() == 0 && s.data() == nullptr)); - - span cs{nullptr}; - CHECK((cs.size() == 0 && cs.data() == nullptr)); - } -} - TEST_CASE("from_nullptr_size_constructor") { { @@ -1192,8 +1150,8 @@ TEST_CASE("crbegin_crend") TEST_CASE("comparison_operators") { { - span s1 = nullptr; - span s2 = nullptr; + span s1; + span s2; CHECK(s1 == s2); CHECK(!(s1 != s2)); CHECK(!(s1 < s2)); @@ -1230,7 +1188,7 @@ TEST_CASE("comparison_operators") { int arr[] = {2, 1}; // bigger - span s1 = nullptr; + span s1; span s2 = arr; CHECK(s1 != s2); diff --git a/tests/string_span_tests.cpp b/tests/string_span_tests.cpp index 83171ac..0032b0f 100644 --- a/tests/string_span_tests.cpp +++ b/tests/string_span_tests.cpp @@ -511,12 +511,6 @@ TEST_CASE("Constructors") CHECK(span.length() == 0); } - // from nullptr - { - cstring_span<> span(nullptr); - CHECK(span.length() == 0); - } - // from string literal { cstring_span<> span = "Hello";