[PATCH] D79740: Align mapped_iterator::reference type with mapped_iterator::operator*() return value.
Ivan Kelarev via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue May 12 10:44:10 PDT 2020
ikelarev updated this revision to Diff 263470.
ikelarev added a comment.
Thanks, David, I believe I see your point. I added a separated implementation of mapped_iterator for the case when functor returns non-reference value. As a reference implementation I used existing pointer_iterator class, which has mutable Ptr member updated in operator*() call.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D79740/new/
https://reviews.llvm.org/D79740
Files:
llvm/include/llvm/ADT/STLExtras.h
Index: llvm/include/llvm/ADT/STLExtras.h
===================================================================
--- llvm/include/llvm/ADT/STLExtras.h
+++ llvm/include/llvm/ADT/STLExtras.h
@@ -285,15 +285,16 @@
template <typename ItTy, typename FuncTy,
typename FuncReturnTy =
- decltype(std::declval<FuncTy>()(*std::declval<ItTy>()))>
+ decltype(std::declval<FuncTy>()(*std::declval<ItTy>())),
+ bool IsReference = std::is_reference<FuncReturnTy>::value>
class mapped_iterator
: public iterator_adaptor_base<
- mapped_iterator<ItTy, FuncTy>, ItTy,
- typename std::iterator_traits<ItTy>::iterator_category,
- typename std::remove_reference<FuncReturnTy>::type> {
+ mapped_iterator<ItTy, FuncTy>, ItTy,
+ typename std::iterator_traits<ItTy>::iterator_category,
+ typename std::remove_reference<FuncReturnTy>::type> {
public:
mapped_iterator(ItTy U, FuncTy F)
- : mapped_iterator::iterator_adaptor_base(std::move(U)), F(std::move(F)) {}
+ : mapped_iterator::iterator_adaptor_base(std::move(U)), F(std::move(F)) {}
ItTy getCurrent() { return this->I; }
@@ -303,6 +304,29 @@
FuncTy F;
};
+template <typename ItTy, typename FuncTy>
+class mapped_iterator<ItTy, FuncTy,
+ decltype(std::declval<FuncTy>()(*std::declval<ItTy>())),
+ false>
+ : public iterator_adaptor_base<
+ mapped_iterator<ItTy, FuncTy>, ItTy,
+ typename std::iterator_traits<ItTy>::iterator_category,
+ decltype(std::declval<FuncTy>()(*std::declval<ItTy>()))> {
+public:
+ mapped_iterator(ItTy U, FuncTy F)
+ : mapped_iterator::iterator_adaptor_base(std::move(U)), F(std::move(F)) {}
+
+ ItTy getCurrent() { return this->I; }
+
+ typename mapped_iterator::iterator_adaptor_base::reference operator*() const {
+ return V = F(*this->I);
+ }
+
+private:
+ FuncTy F;
+ mutable typename mapped_iterator::iterator_adaptor_base::value_type V;
+};
+
// map_iterator - Provide a convenient way to create mapped_iterators, just like
// make_pair is useful for creating pairs...
template <class ItTy, class FuncTy>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79740.263470.patch
Type: text/x-patch
Size: 2192 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200512/5cef7e41/attachment.bin>
More information about the llvm-commits
mailing list