diff options
Diffstat (limited to 'llvm/include/llvm/ADT/iterator.h')
-rw-r--r-- | llvm/include/llvm/ADT/iterator.h | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/llvm/include/llvm/ADT/iterator.h b/llvm/include/llvm/ADT/iterator.h index c3079289270..2898a677db3 100644 --- a/llvm/include/llvm/ADT/iterator.h +++ b/llvm/include/llvm/ADT/iterator.h @@ -46,6 +46,22 @@ protected: std::is_base_of<std::bidirectional_iterator_tag, IteratorCategoryT>::value, }; + /// A proxy object for computing a reference via indirecting a copy of an + /// iterator. This is used in APIs which need to produce a reference via + /// indirection but for which the iterator object might be a temporary. The + /// proxy preserves the iterator internally and exposes the indirected + /// reference via a conversion operator. + class ReferenceProxy { + friend iterator_facade_base; + + DerivedT I; + + ReferenceProxy(DerivedT I) : I(std::move(I)) {} + + public: + operator ReferenceT() const { return *I; } + }; + public: DerivedT operator+(DifferenceTypeT n) const { static_assert( @@ -120,10 +136,10 @@ public: PointerT operator->() const { return &static_cast<const DerivedT *>(this)->operator*(); } - ReferenceT operator[](DifferenceTypeT n) const { + ReferenceProxy operator[](DifferenceTypeT n) const { static_assert(IsRandomAccess, "Subscripting is only defined for random access iterators."); - return *static_cast<const DerivedT *>(this)->operator+(n); + return ReferenceProxy(static_cast<const DerivedT *>(this)->operator+(n)); } }; |