First part of P1024: Usability Enhancements for std::span. Remove operator() for indexing, and add 'front' and 'back' calls.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@354801 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow
2019-02-25 17:54:08 +00:00
parent 1c2f15d4c4
commit e5cb744bf1
4 changed files with 175 additions and 15 deletions

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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{};