Add a deduction guide for base::span from an iterator and a size.
Bug: 1490484 Change-Id: Ib6427f8a351f0723d9c59e31f1d0b64dcc0c1470 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4918886 Reviewed-by: Peter Kasting <pkasting@chromium.org> Commit-Queue: Daniel Cheng <dcheng@chromium.org> Cr-Commit-Position: refs/heads/main@{#1207147}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
a49173cd13
commit
cfb0f12529
base/containers
@ -460,6 +460,10 @@ class GSL_POINTER span : public internal::ExtentStorage<Extent> {
|
||||
template <class T, size_t Extent>
|
||||
constexpr size_t span<T, Extent>::extent;
|
||||
|
||||
template <typename It,
|
||||
typename T = std::remove_reference_t<iter_reference_t<It>>>
|
||||
span(It it, StrictNumeric<size_t> size) -> span<T>;
|
||||
|
||||
template <typename Container,
|
||||
typename T = std::remove_pointer_t<
|
||||
decltype(std::data(std::declval<Container>()))>,
|
||||
|
@ -27,6 +27,38 @@ using ::testing::Pointwise;
|
||||
|
||||
namespace base {
|
||||
|
||||
namespace {
|
||||
|
||||
// Tests for span(It, StrictNumeric<size_t>) deduction guide. These tests use a
|
||||
// helper function to wrap the static_asserts, as most STL containers don't work
|
||||
// well in a constexpr context. std::array<T, N> does, but base::span has
|
||||
// specific overloads for std::array<T, n>, so that ends up being less helpful
|
||||
// than it would initially appear.
|
||||
//
|
||||
// Another alternative would be to use std::declval, but that would be fairly
|
||||
// verbose.
|
||||
[[maybe_unused]] void DeductionGuidesWithIteratorAndSize() {
|
||||
{
|
||||
const std::vector<int> v;
|
||||
static_assert(
|
||||
std::is_same_v<decltype(span(v.cbegin(), v.size())), span<const int>>);
|
||||
static_assert(
|
||||
std::is_same_v<decltype(span(v.begin(), v.size())), span<const int>>);
|
||||
static_assert(
|
||||
std::is_same_v<decltype(span(v.data(), v.size())), span<const int>>);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v;
|
||||
static_assert(
|
||||
std::is_same_v<decltype(span(v.cbegin(), v.size())), span<const int>>);
|
||||
static_assert(
|
||||
std::is_same_v<decltype(span(v.begin(), v.size())), span<int>>);
|
||||
static_assert(
|
||||
std::is_same_v<decltype(span(v.data(), v.size())), span<int>>);
|
||||
}
|
||||
}
|
||||
|
||||
// Tests for span(Container&&) deduction guide.
|
||||
static_assert(std::is_same_v<decltype(span(std::declval<const std::string&>())),
|
||||
span<const char>>);
|
||||
@ -60,6 +92,8 @@ static_assert(
|
||||
std::is_same_v<decltype(span(std::declval<std::array<float, 9>&&>())),
|
||||
span<const float, 9>>);
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(SpanTest, DefaultConstructor) {
|
||||
span<int> dynamic_span;
|
||||
EXPECT_EQ(nullptr, dynamic_span.data());
|
||||
|
Reference in New Issue
Block a user