[llvm] r364007 - Use std::iterator_traits to infer result type of llvm::enumerate iterator wrapper

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 20 22:43:08 PDT 2019


Author: mehdi_amini
Date: Thu Jun 20 22:43:08 2019
New Revision: 364007

URL: http://llvm.org/viewvc/llvm-project?rev=364007&view=rev
Log:
Use std::iterator_traits to infer result type of llvm::enumerate iterator wrapper

Update the llvm::enumerate helper class result_pair<R> to use the 'iterator_traits<R>::reference'
type as the result of 'value()' instead 'ValueOfRange<R> &'. This enables support for iterators
that return value types, i.e. non reference. This is a common pattern for some classes of
iterators, e.g. mapped_iterator.

Patch by: River Riddle <riverriddle at google.com>

Differential Revision: https://reviews.llvm.org/D63632

Modified:
    llvm/trunk/include/llvm/ADT/STLExtras.h

Modified: llvm/trunk/include/llvm/ADT/STLExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/STLExtras.h?rev=364007&r1=364006&r2=364007&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/STLExtras.h (original)
+++ llvm/trunk/include/llvm/ADT/STLExtras.h Thu Jun 20 22:43:08 2019
@@ -1506,6 +1506,9 @@ namespace detail {
 template <typename R> class enumerator_iter;
 
 template <typename R> struct result_pair {
+  using value_reference =
+      typename std::iterator_traits<IterOfRange<R>>::reference;
+
   friend class enumerator_iter<R>;
 
   result_pair() = default;
@@ -1519,8 +1522,8 @@ template <typename R> struct result_pair
   }
 
   std::size_t index() const { return Index; }
-  const ValueOfRange<R> &value() const { return *Iter; }
-  ValueOfRange<R> &value() { return *Iter; }
+  const value_reference value() const { return *Iter; }
+  value_reference value() { return *Iter; }
 
 private:
   std::size_t Index = std::numeric_limits<std::size_t>::max();




More information about the llvm-commits mailing list