From b186b6cc680ffd6a4d18794674ee1eeadeb3af87 Mon Sep 17 00:00:00 2001 From: Martijn Otto Date: Thu, 14 Nov 2019 13:13:19 +0100 Subject: [PATCH 1/2] 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 2/2] 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};