diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2016-07-23 20:41:05 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2016-07-23 20:41:05 +0000 |
commit | 01095a5d43bbfde13731688ddcf6048ebb8b7721 (patch) | |
tree | 4def12e759965de927d963ac65840d663ef9d1ea /include/llvm/ADT/iterator.h | |
parent | f0f4822ed4b66e3579e92a89f368f8fb860e218e (diff) |
Diffstat (limited to 'include/llvm/ADT/iterator.h')
-rw-r--r-- | include/llvm/ADT/iterator.h | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/include/llvm/ADT/iterator.h b/include/llvm/ADT/iterator.h index c307928927038..2898a677db371 100644 --- a/include/llvm/ADT/iterator.h +++ b/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)); } }; |