[libcxx-commits] [libcxx] r354801 - First part of P1024: Usability Enhancements for std::span. Remove operator() for indexing, and add 'front' and 'back' calls.

Marshall Clow via libcxx-commits libcxx-commits at lists.llvm.org
Mon Feb 25 09:54:08 PST 2019


Author: marshall
Date: Mon Feb 25 09:54:08 2019
New Revision: 354801

URL: http://llvm.org/viewvc/llvm-project?rev=354801&view=rev
Log:
First part of P1024: Usability Enhancements for std::span. Remove operator() for indexing, and add 'front' and 'back' calls.

Added:
    libcxx/trunk/test/std/containers/views/span.elem/back.pass.cpp
    libcxx/trunk/test/std/containers/views/span.elem/front.pass.cpp
Modified:
    libcxx/trunk/include/span
    libcxx/trunk/test/std/containers/views/span.elem/op_idx.pass.cpp

Modified: libcxx/trunk/include/span
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/span?rev=354801&r1=354800&r2=354801&view=diff
==============================================================================
--- libcxx/trunk/include/span (original)
+++ libcxx/trunk/include/span Mon Feb 25 09:54:08 2019
@@ -88,7 +88,8 @@ public:
 
     // [span.elem], span element access
     constexpr reference operator[](index_type idx) const;
-    constexpr reference operator()(index_type idx) const;
+    constexpr reference front() const;
+    constexpr reference back() const;
     constexpr pointer data() const noexcept;
 
     // [span.iterators], span iterator support
@@ -319,10 +320,16 @@ public:
         return __data[__idx];
     }
 
-    _LIBCPP_INLINE_VISIBILITY constexpr reference operator()(index_type __idx) const noexcept
+    _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
     {
-        _LIBCPP_ASSERT(__idx >= 0 && __idx < size(), "span<T,N>() index out of bounds");
-        return __data[__idx];
+        static_assert(_Extent > 0, "span<T,N>[].front() on empty span");
+        return __data[0];
+    }
+
+    _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
+    {
+        static_assert(_Extent > 0, "span<T,N>[].back() on empty span");
+        return __data[size()-1];
     }
 
     _LIBCPP_INLINE_VISIBILITY constexpr pointer data()                         const noexcept { return __data; }
@@ -484,12 +491,19 @@ public:
         return __data[__idx];
     }
 
-    _LIBCPP_INLINE_VISIBILITY constexpr reference operator()(index_type __idx) const noexcept
+    _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
     {
-        _LIBCPP_ASSERT(__idx >= 0 && __idx < size(), "span<T>() index out of bounds");
-        return __data[__idx];
+        _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span");
+        return __data[0];
     }
 
+    _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
+    {
+        _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span");
+        return __data[size()-1];
+    }
+
+
     _LIBCPP_INLINE_VISIBILITY constexpr pointer data()                         const noexcept { return __data; }
 
 // [span.iter], span iterator support

Added: libcxx/trunk/test/std/containers/views/span.elem/back.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/views/span.elem/back.pass.cpp?rev=354801&view=auto
==============================================================================
--- libcxx/trunk/test/std/containers/views/span.elem/back.pass.cpp (added)
+++ libcxx/trunk/test/std/containers/views/span.elem/back.pass.cpp Mon Feb 25 09:54:08 2019
@@ -0,0 +1,75 @@
+// -*- C++ -*-
+//===------------------------------ span ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===---------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
+
+// <span>
+
+// constexpr reference back() const noexcept;
+//   Expects: empty() is false.
+//   Effects: Equivalent to: return *(data() + (size() - 1));
+//
+
+
+#include <span>
+#include <cassert>
+#include <string>
+
+#include "test_macros.h"
+
+
+template <typename Span>
+constexpr bool testConstexprSpan(Span sp)
+{
+    _LIBCPP_ASSERT(sp.back(), "");
+    return std::addressof(sp.back()) == sp.data() + sp.size() - 1;
+}
+
+
+template <typename Span>
+void testRuntimeSpan(Span sp)
+{
+    _LIBCPP_ASSERT(sp.back(), "");
+    assert(std::addressof(sp.back()) == sp.data() + sp.size() - 1);
+}
+
+
+struct A{};
+constexpr int iArr1[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9};
+          int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
+
+int main(int, char**)
+{
+    static_assert(testConstexprSpan(std::span<const int>(iArr1, 1)), "");
+    static_assert(testConstexprSpan(std::span<const int>(iArr1, 2)), "");
+    static_assert(testConstexprSpan(std::span<const int>(iArr1, 3)), "");
+    static_assert(testConstexprSpan(std::span<const int>(iArr1, 4)), "");
+
+    static_assert(testConstexprSpan(std::span<const int, 1>(iArr1, 1)), "");
+    static_assert(testConstexprSpan(std::span<const int, 2>(iArr1, 2)), "");
+    static_assert(testConstexprSpan(std::span<const int, 3>(iArr1, 3)), "");
+    static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4)), "");
+
+
+    testRuntimeSpan(std::span<int>(iArr2, 1));
+    testRuntimeSpan(std::span<int>(iArr2, 2));
+    testRuntimeSpan(std::span<int>(iArr2, 3));
+    testRuntimeSpan(std::span<int>(iArr2, 4));
+
+
+    testRuntimeSpan(std::span<int, 1>(iArr2, 1));
+    testRuntimeSpan(std::span<int, 2>(iArr2, 2));
+    testRuntimeSpan(std::span<int, 3>(iArr2, 3));
+    testRuntimeSpan(std::span<int, 4>(iArr2, 4));
+
+    std::string s;
+    testRuntimeSpan(std::span<std::string>   (&s, 1));
+    testRuntimeSpan(std::span<std::string, 1>(&s, 1));
+
+  return 0;
+}

Added: libcxx/trunk/test/std/containers/views/span.elem/front.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/views/span.elem/front.pass.cpp?rev=354801&view=auto
==============================================================================
--- libcxx/trunk/test/std/containers/views/span.elem/front.pass.cpp (added)
+++ libcxx/trunk/test/std/containers/views/span.elem/front.pass.cpp Mon Feb 25 09:54:08 2019
@@ -0,0 +1,75 @@
+// -*- C++ -*-
+//===------------------------------ span ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===---------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
+
+// <span>
+
+// constexpr reference front() const noexcept;
+//   Expects: empty() is false.
+//   Effects: Equivalent to: return *data();
+//
+
+
+#include <span>
+#include <cassert>
+#include <string>
+
+#include "test_macros.h"
+
+
+template <typename Span>
+constexpr bool testConstexprSpan(Span sp)
+{
+    _LIBCPP_ASSERT(sp.front(), "");
+    return std::addressof(sp.front()) == sp.data();
+}
+
+
+template <typename Span>
+void testRuntimeSpan(Span sp)
+{
+    _LIBCPP_ASSERT(sp.front(), "");
+    assert(std::addressof(sp.front()) == sp.data());
+}
+
+
+struct A{};
+constexpr int iArr1[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9};
+          int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
+
+int main(int, char**)
+{
+    static_assert(testConstexprSpan(std::span<const int>(iArr1, 1)), "");
+    static_assert(testConstexprSpan(std::span<const int>(iArr1, 2)), "");
+    static_assert(testConstexprSpan(std::span<const int>(iArr1, 3)), "");
+    static_assert(testConstexprSpan(std::span<const int>(iArr1, 4)), "");
+
+    static_assert(testConstexprSpan(std::span<const int, 1>(iArr1, 1)), "");
+    static_assert(testConstexprSpan(std::span<const int, 2>(iArr1, 2)), "");
+    static_assert(testConstexprSpan(std::span<const int, 3>(iArr1, 3)), "");
+    static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4)), "");
+
+
+    testRuntimeSpan(std::span<int>(iArr2, 1));
+    testRuntimeSpan(std::span<int>(iArr2, 2));
+    testRuntimeSpan(std::span<int>(iArr2, 3));
+    testRuntimeSpan(std::span<int>(iArr2, 4));
+
+
+    testRuntimeSpan(std::span<int, 1>(iArr2, 1));
+    testRuntimeSpan(std::span<int, 2>(iArr2, 2));
+    testRuntimeSpan(std::span<int, 3>(iArr2, 3));
+    testRuntimeSpan(std::span<int, 4>(iArr2, 4));
+
+    std::string s;
+    testRuntimeSpan(std::span<std::string>   (&s, 1));
+    testRuntimeSpan(std::span<std::string, 1>(&s, 1));
+
+  return 0;
+}

Modified: libcxx/trunk/test/std/containers/views/span.elem/op_idx.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/views/span.elem/op_idx.pass.cpp?rev=354801&r1=354800&r2=354801&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/views/span.elem/op_idx.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/views/span.elem/op_idx.pass.cpp Mon Feb 25 09:54:08 2019
@@ -26,12 +26,10 @@ template <typename Span>
 constexpr bool testConstexprSpan(Span sp, ptrdiff_t idx)
 {
     _LIBCPP_ASSERT(noexcept(sp[idx]), "");
-    _LIBCPP_ASSERT(noexcept(sp(idx)), "");
 
     typename Span::reference r1 = sp[idx];
-    typename Span::reference r2 = sp(idx);
-    typename Span::reference r3 = *(sp.data() + idx);
-    return r1 == r2 && r2 == r3;
+    typename Span::reference r2 = *(sp.data() + idx);
+    return r1 == r2;
 }
 
 
@@ -39,12 +37,10 @@ template <typename Span>
 void testRuntimeSpan(Span sp, ptrdiff_t idx)
 {
     _LIBCPP_ASSERT(noexcept(sp[idx]), "");
-    _LIBCPP_ASSERT(noexcept(sp(idx)), "");
 
     typename Span::reference r1 = sp[idx];
-    typename Span::reference r2 = sp(idx);
-    typename Span::reference r3 = *(sp.data() + idx);
-    assert(r1 == r2 && r2 == r3);
+    typename Span::reference r2 = *(sp.data() + idx);
+    assert(r1 == r2);
 }
 
 struct A{};




More information about the libcxx-commits mailing list