From 87e22faa7e3643a1b03d5ad8ee60c2f88af2047a Mon Sep 17 00:00:00 2001 From: Neil MacIntosh Date: Sat, 10 Feb 2018 20:02:05 -0800 Subject: [PATCH 1/5] Removed from-smart-ptr constructors. (#611) As per feedback during standardization discussions. --- 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 c23d17a61a6ac1a446494026d94c2988aadd5258 Mon Sep 17 00:00:00 2001 From: Neil MacIntosh Date: Sat, 10 Feb 2018 21:11:08 -0800 Subject: [PATCH 2/5] Make iterator access functions on span constexpr (#612) As per feedback on span received during the C++ standardization discussions. --- 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 95da2fd337b164139bba4ad178f263aad831a010 Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Sun, 11 Feb 2018 12:16:39 -0800 Subject: [PATCH 3/5] Don't use clang's __builtin_assume as an optimizer hint (#608) Fixes #607. Drive-by: use '?:' to contextually convert to bool in the fallback definition of `GSL_ASSUME` --- include/gsl/gsl_assert | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/include/gsl/gsl_assert b/include/gsl/gsl_assert index 468d4a0..eeb3473 100644 --- a/include/gsl/gsl_assert +++ b/include/gsl/gsl_assert @@ -52,12 +52,10 @@ // #ifdef _MSC_VER #define GSL_ASSUME(cond) __assume(cond) -#elif defined(__clang__) -#define GSL_ASSUME(cond) __builtin_assume(cond) #elif defined(__GNUC__) #define GSL_ASSUME(cond) ((cond) ? static_cast(0) : __builtin_unreachable()) #else -#define GSL_ASSUME(cond) static_cast(!!(cond)) +#define GSL_ASSUME(cond) static_cast((cond) ? 0 : 0) #endif // From 1a3eb2a6d44f072f31a43e71e123dc4c4ce3b289 Mon Sep 17 00:00:00 2001 From: Beat Bolli Date: Sun, 11 Feb 2018 21:18:00 +0100 Subject: [PATCH 4/5] Rename final_act in GSL.natvis (#593) Commit 6e2e207c8d renamed final_act to final_action. Do this also in GSL.natvis. --- GSL.natvis | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GSL.natvis b/GSL.natvis index e40a8c9..a467a17 100644 --- a/GSL.natvis +++ b/GSL.natvis @@ -11,7 +11,7 @@ - + {{ invoke = {invoke_}, action = {f_} }} invoke_ From 3ff6004bc3c2c8a2d578379ac355d600fc875a8f Mon Sep 17 00:00:00 2001 From: beinhaerter <34543625+beinhaerter@users.noreply.github.com> Date: Sun, 11 Feb 2018 21:19:10 +0100 Subject: [PATCH 5/5] added const (#605) Resolves compiler code analysis warning ...\gsl\include\gsl\string_span(101): warning C26496: The variable 'str_span' is assigned only once, mark it as const (con.4: https://go.microsoft.com/fwlink/p/?LinkID=784969). --- include/gsl/string_span | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/gsl/string_span b/include/gsl/string_span index fa8b515..3c40aaa 100644 --- a/include/gsl/string_span +++ b/include/gsl/string_span @@ -98,7 +98,7 @@ namespace details { if (str == nullptr || n <= 0) return 0; - span str_span{str, n}; + const span str_span{str, n}; std::ptrdiff_t len = 0; while (len < n && str_span[len]) len++;