Skip to content

[ArrayRef] Add constructor from iterator_range<U*> (NFC). #137796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions llvm/include/llvm/ADT/ArrayRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ namespace llvm {
* = nullptr)
: Data(Vec.data()), Length(Vec.size()) {}

/// Construct an ArrayRef<T> from iterator_range<U*>. This uses SFINAE
/// to ensure that this is only used for iterator ranges over plain pointer
/// iterators.
template <typename U, typename = std::enable_if_t<
std::is_convertible_v<U *const *, T *const *>>>
ArrayRef(const iterator_range<U *> &Range)
: Data(Range.begin()), Length(llvm::size(Range)) {}

/// @}
/// @name Simple Operations
/// @{
Expand Down
58 changes: 58 additions & 0 deletions llvm/unittests/ADT/ArrayRefTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,64 @@ TEST(ArrayRefTest, ArrayRefFromStdArray) {
}
}

struct TestRandomAccessIterator {
using iterator_category = std::random_access_iterator_tag;
};

static_assert(!std::is_constructible_v<
ArrayRef<int>, iterator_range<TestRandomAccessIterator>>,
"cannot construct from iterator range with non-pointer iterator");
static_assert(!std::is_constructible_v<ArrayRef<int>, iterator_range<int>>,
"cannot construct from iterator range with non-pointer iterator");

class TestBase {};

class TestDerived : public TestBase {};

static_assert(
!std::is_constructible_v<ArrayRef<TestDerived>, iterator_range<TestBase *>>,
"cannot construct ArrayRef with derived type");
static_assert(
!std::is_constructible_v<ArrayRef<TestBase>, iterator_range<TestDerived *>>,
"cannot construct ArrayRef base type");
static_assert(!std::is_constructible_v<ArrayRef<TestBase *>,
iterator_range<TestDerived **>>,
"cannot construct ArrayRef pointer of base type");

static_assert(
!std::is_constructible_v<ArrayRef<int>, iterator_range<const int *>>,
"cannot construct ArrayRef with non-const elements from const iterator "
"range");
static_assert(
std::is_constructible_v<ArrayRef<char *>, iterator_range<char **>>,
"should be able to construct ArrayRef from iterator_range over pointers");
static_assert(
!std::is_constructible_v<ArrayRef<char *>, iterator_range<char *const *>>,
"should be able to construct ArrayRef from iterator_range over pointers");

TEST(ArrayRefTest, ArrayRefFromIteratorRange) {
std::array<int, 5> A1{{42, -5, 0, 1000000, -1000000}};
ArrayRef<int> A2 = make_range(A1.begin(), A1.end());

EXPECT_EQ(A1.size(), A2.size());
for (std::size_t i = 0; i < A1.size(); ++i)
EXPECT_EQ(A1[i], A2[i]);

ArrayRef<const int> A3 = make_range(A1.begin(), A1.end());
EXPECT_EQ(A1.size(), A3.size());
for (std::size_t i = 0; i < A1.size(); ++i)
EXPECT_EQ(A1[i], A3[i]);
}

TEST(ArrayRefTest, ArrayRefFromIteratorConstRange) {
std::array<const int, 5> A1{{42, -5, 0, 1000000, -1000000}};
ArrayRef<const int> A2 = make_range(A1.begin(), A1.end());

EXPECT_EQ(A1.size(), A2.size());
for (std::size_t i = 0; i < A1.size(); ++i)
EXPECT_EQ(A1[i], A2[i]);
}

static_assert(std::is_trivially_copyable_v<ArrayRef<int>>,
"trivially copyable");

Expand Down
Loading