-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[ADT] Use adl_begin/end with to_vector functions
#164823
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
Conversation
Similar to other code in ADT / STLExtras, allow `to_vector` to work with ranges that require ADL to find the begin/end iterators.
|
@llvm/pr-subscribers-llvm-adt Author: Jakub Kuderski (kuhar) ChangesSimilar to other code in ADT / STLExtras, allow Full diff: https://github.com/llvm/llvm-project/pull/164823.diff 2 Files Affected:
diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h
index ca0b918f56c46..51109d1f416f2 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -14,6 +14,7 @@
#ifndef LLVM_ADT_SMALLVECTOR_H
#define LLVM_ADT_SMALLVECTOR_H
+#include "llvm/ADT/ADL.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/Support/Compiler.h"
#include <algorithm>
@@ -1295,28 +1296,27 @@ inline size_t capacity_in_bytes(const SmallVector<T, N> &X) {
template <typename RangeType>
using ValueTypeFromRangeType =
- std::remove_const_t<std::remove_reference_t<decltype(*std::begin(
- std::declval<RangeType &>()))>>;
+ std::remove_const_t<detail::ValueOfRange<RangeType>>;
/// Given a range of type R, iterate the entire range and return a
/// SmallVector with elements of the vector. This is useful, for example,
/// when you want to iterate a range and then sort the results.
template <unsigned Size, typename R>
SmallVector<ValueTypeFromRangeType<R>, Size> to_vector(R &&Range) {
- return {std::begin(Range), std::end(Range)};
+ return {adl_begin(Range), adl_end(Range)};
}
template <typename R>
SmallVector<ValueTypeFromRangeType<R>> to_vector(R &&Range) {
- return {std::begin(Range), std::end(Range)};
+ return {adl_begin(Range), adl_end(Range)};
}
template <typename Out, unsigned Size, typename R>
SmallVector<Out, Size> to_vector_of(R &&Range) {
- return {std::begin(Range), std::end(Range)};
+ return {adl_begin(Range), adl_end(Range)};
}
template <typename Out, typename R> SmallVector<Out> to_vector_of(R &&Range) {
- return {std::begin(Range), std::end(Range)};
+ return {adl_begin(Range), adl_end(Range)};
}
// Explicit instantiations
diff --git a/llvm/unittests/ADT/SmallVectorTest.cpp b/llvm/unittests/ADT/SmallVectorTest.cpp
index b216359ffd31c..d790f02d16ac8 100644
--- a/llvm/unittests/ADT/SmallVectorTest.cpp
+++ b/llvm/unittests/ADT/SmallVectorTest.cpp
@@ -13,6 +13,7 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/Compiler.h"
+#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <list>
#include <stdarg.h>
@@ -1156,6 +1157,15 @@ TEST(SmallVectorTest, InitializerList) {
EXPECT_TRUE(ArrayRef(V2).equals({4, 5, 3, 2}));
}
+namespace namespace_with_adl {
+struct MyVector {
+ std::vector<int> data;
+};
+
+std::vector<int>::const_iterator begin(MyVector &v) { return v.data.begin(); }
+std::vector<int>::const_iterator end(MyVector &v) { return v.data.end(); }
+} // namespace namespace_with_adl
+
TEST(SmallVectorTest, ToVector) {
{
std::vector<char> v = {'a', 'b', 'c'};
@@ -1173,6 +1183,15 @@ TEST(SmallVectorTest, ToVector) {
for (size_t I = 0; I < v.size(); ++I)
EXPECT_EQ(v[I], Vector[I]);
}
+ {
+ // Check that to_vector and to_vector_of work with types that require ADL
+ // for being/end iterators.
+ namespace_with_adl::MyVector V = {{1, 2, 3}};
+ auto IntVector = to_vector(V);
+ EXPECT_THAT(IntVector, testing::ElementsAre(1, 2, 3));
+ IntVector = to_vector<3>(V);
+ EXPECT_THAT(IntVector, testing::ElementsAre(1, 2, 3));
+ }
}
struct To {
@@ -1231,6 +1250,15 @@ TEST(SmallVectorTest, ToVectorOf) {
for (size_t I = 0; I < StdVector.size(); ++I)
EXPECT_EQ(StdVector[I], Vector[I]);
}
+ {
+ // Check that to_vector works with types that require ADL for being/end
+ // iterators.
+ namespace_with_adl::MyVector V = {{1, 2, 3}};
+ auto UnsignedVector = to_vector_of<unsigned>(V);
+ EXPECT_THAT(UnsignedVector, testing::ElementsAre(1u, 2u, 3u));
+ UnsignedVector = to_vector_of<unsigned, 3>(V);
+ EXPECT_THAT(UnsignedVector, testing::ElementsAre(1u, 2u, 3u));
+ }
}
template <class VectorT>
|
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/88/builds/17442 Here is the relevant piece of the build log for the reference |
Similar to other code in ADT / STLExtras, allow `to_vector` to work with ranges that require ADL to find the begin/end iterators.
Similar to other code in ADT / STLExtras, allow `to_vector` to work with ranges that require ADL to find the begin/end iterators.
Similar to other code in ADT / STLExtras, allow `to_vector` to work with ranges that require ADL to find the begin/end iterators.
Similar to other code in ADT / STLExtras, allow
to_vectorto work with ranges that require ADL to find the begin/end iterators.