Fix reverse to work on const rbegin()/rend().

Duncan found that reverse worked on mutable rbegin(), but the has_rbegin
trait didn't work with a const method.  See http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20160815/382890.html
for more details.

Turns out this was already solved in clang with has_getDecl.  Copied that and made it work for rbegin.

This includes the tests Duncan attached to that thread, including the traits test.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@278991 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Pete Cooper 2016-08-17 22:06:59 +00:00
parent e65e275cf2
commit cebbb5ed8d
2 changed files with 43 additions and 4 deletions

View File

@ -209,10 +209,19 @@ inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
}
/// \brief Metafunction to determine if type T has a member called rbegin().
template <typename T> struct has_rbegin {
template <typename U> static char(&f(const U &, decltype(&U::rbegin)))[1];
static char(&f(...))[2];
const static bool value = sizeof(f(std::declval<T>(), nullptr)) == 1;
template <typename Ty>
class has_rbegin {
typedef char yes[1];
typedef char no[2];
template <typename Inner>
static yes& test(Inner *I, decltype(I->rbegin()) * = nullptr);
template <typename>
static no& test(...);
public:
static const bool value = sizeof(test<Ty>(nullptr)) == sizeof(yes);
};
// Returns an iterator_range over the given container which iterates in reverse.

View File

@ -27,8 +27,11 @@ public:
ReverseOnlyVector(std::initializer_list<int> list) : Vec(list) {}
typedef std::vector<int>::reverse_iterator reverse_iterator;
typedef std::vector<int>::const_reverse_iterator const_reverse_iterator;
reverse_iterator rbegin() { return Vec.rbegin(); }
reverse_iterator rend() { return Vec.rend(); }
const_reverse_iterator rbegin() const { return Vec.rbegin(); }
const_reverse_iterator rend() const { return Vec.rend(); }
};
// A wrapper around vector which exposes begin(), end(), rbegin() and rend().
@ -49,6 +52,29 @@ public:
reverse_iterator rend() { return Vec.rend(); }
};
/// This is the same as BidirectionalVector but with the addition of const
/// begin/rbegin methods to ensure that the type traits for has_rbegin works.
class BidirectionalVectorConsts {
std::vector<int> Vec;
public:
BidirectionalVectorConsts(std::initializer_list<int> list) : Vec(list) {}
typedef std::vector<int>::iterator iterator;
typedef std::vector<int>::const_iterator const_iterator;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
typedef std::vector<int>::reverse_iterator reverse_iterator;
typedef std::vector<int>::const_reverse_iterator const_reverse_iterator;
reverse_iterator rbegin() { return Vec.rbegin(); }
reverse_iterator rend() { return Vec.rend(); }
const_reverse_iterator rbegin() const { return Vec.rbegin(); }
const_reverse_iterator rend() const { return Vec.rend(); }
};
template <typename R> void TestRev(const R &r) {
int counter = 3;
for (int i : r)
@ -80,4 +106,8 @@ TYPED_TEST(RangeAdapterRValueTest, TrivialOperation) {
TestRev(reverse(TypeParam({0, 1, 2, 3})));
}
TYPED_TEST(RangeAdapterRValueTest, HasRbegin) {
EXPECT_TRUE(has_rbegin<TypeParam>::value);
}
} // anonymous namespace