[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
Mon May 11 13:28:37 PDT 2020


ikelarev created this revision.
ikelarev added reviewers: mehdi_amini, rriddle, dblaikie, timshen, lhames, chandlerc.
Herald added subscribers: llvm-commits, dexonsmith.
Herald added a project: LLVM.

This change is to provide FuncReturnTy as a reference type to
iterator_adaptor_base class. It fixes a compilation error when
mapped_iterator is wrapped into another iterator object (e.g.
reverse_iterator) and its operator*() tries to return a reference type.
Actually mapped_iterator could not provide a references to the values it
iterates on, which leads to the compilation error.

Example:

  std::vector<int> V;
  auto R1 = map_range(V, ([](int) { return 0; }));
  auto R2 = reverse(R1);
  *R1.begin(); // OK
  *R2.begin(); // error C2440: 'return': cannot convert from
               // 'FuncReturnTy' to 'int &' with FuncReturnTy=int


Repository:
  rG LLVM Github Monorepo

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
@@ -290,7 +290,15 @@
     : public iterator_adaptor_base<
              mapped_iterator<ItTy, FuncTy>, ItTy,
              typename std::iterator_traits<ItTy>::iterator_category,
-             typename std::remove_reference<FuncReturnTy>::type> {
+             typename std::remove_reference<FuncReturnTy>::type,
+             typename std::iterator_traits<ItTy>::difference_type,
+             std::conditional_t<
+                 std::is_same<
+                     typename std::remove_reference<FuncReturnTy>::type,
+                     typename std::iterator_traits<ItTy>::value_type>::value,
+                 typename std::iterator_traits<ItTy>::pointer,
+                 typename std::remove_reference<FuncReturnTy>::type *>,
+             FuncReturnTy> {
 public:
   mapped_iterator(ItTy U, FuncTy F)
     : mapped_iterator::iterator_adaptor_base(std::move(U)), F(std::move(F)) {}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79740.263251.patch
Type: text/x-patch
Size: 1078 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200511/62ae9241/attachment.bin>


More information about the llvm-commits mailing list