[PATCH] D54377: Correctly instantiate `iterator_adaptor_base` when defining `pointer_iterator`
Dylan MacKenzie via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 13 20:04:45 PST 2018
ecstatic-morse updated this revision to Diff 173979.
ecstatic-morse added a comment.
Added tests for `pointer_iterator` and `pointee_iterator` to ensure that the category of the underlying iterator is forwarded correctly.
https://reviews.llvm.org/D54377
Files:
include/llvm/ADT/iterator.h
unittests/ADT/IteratorTest.cpp
Index: unittests/ADT/IteratorTest.cpp
===================================================================
--- unittests/ADT/IteratorTest.cpp
+++ unittests/ADT/IteratorTest.cpp
@@ -7,6 +7,7 @@
//
//===----------------------------------------------------------------------===//
+#include "llvm/ADT/ilist.h"
#include "llvm/ADT/iterator.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
@@ -35,6 +36,34 @@
static_assert(std::is_same<typename AdaptedIter::reference, Shadow<3>>::value,
"");
+// Ensure that pointe{e,r}_iterator adaptors correctly forward the category of
+// the underlying iterator.
+
+using RandomAccessIter = SmallVectorImpl<int*>::iterator;
+using BidiIter = ilist<int*>::iterator;
+
+template<class T>
+using pointee_iterator_defaulted = pointee_iterator<T>;
+template<class T>
+using pointer_iterator_defaulted = pointer_iterator<T>;
+
+// Ensures that an iterator and its adaptation have the same iterator_category.
+template<template<typename> class A, typename It>
+using IsAdaptedIterCategorySame =
+ std::is_same<typename std::iterator_traits<It>::iterator_category,
+ typename std::iterator_traits<A<It>>::iterator_category>;
+
+// pointeE_iterator
+static_assert(IsAdaptedIterCategorySame<pointee_iterator_defaulted,
+ RandomAccessIter>::value, "");
+static_assert(IsAdaptedIterCategorySame<pointee_iterator_defaulted,
+ BidiIter>::value, "");
+// pointeR_iterator
+static_assert(IsAdaptedIterCategorySame<pointer_iterator_defaulted,
+ RandomAccessIter>::value, "");
+static_assert(IsAdaptedIterCategorySame<pointer_iterator_defaulted,
+ BidiIter>::value, "");
+
TEST(PointeeIteratorTest, Basic) {
int arr[4] = {1, 2, 3, 4};
SmallVector<int *, 4> V;
Index: include/llvm/ADT/iterator.h
===================================================================
--- include/llvm/ADT/iterator.h
+++ include/llvm/ADT/iterator.h
@@ -309,8 +309,10 @@
template <typename WrappedIteratorT,
typename T = decltype(&*std::declval<WrappedIteratorT>())>
class pointer_iterator
- : public iterator_adaptor_base<pointer_iterator<WrappedIteratorT, T>,
- WrappedIteratorT, T> {
+ : public iterator_adaptor_base<
+ pointer_iterator<WrappedIteratorT, T>, WrappedIteratorT,
+ typename std::iterator_traits<WrappedIteratorT>::iterator_category,
+ T> {
mutable T Ptr;
public:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54377.173979.patch
Type: text/x-patch
Size: 2576 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181114/62ab2c15/attachment.bin>
More information about the llvm-commits
mailing list