Add llvm::enumerate() to stl extras

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 29 12:05:26 PDT 2016


> On Sep 29, 2016, at 11:23 AM, Zachary Turner <zturner at google.com> wrote:
> 
> I frequently find myself resorting to index based enumeration because ranged based fors do not allow you to access the index.

Same here!!

>   With llvm::enumerate(), you can write something like this:
> 
> for (auto X : enumerate(my_container)) {
>   if (X.Index == 1)
>     continue;
>   printf("%d", X.Value);
> }
> 
> It handles both const and non-const containers, so that for non-const containers you can modify the underlying sequence item (e.g. the `Value` member of the returned enumerator is a reference).

I like the idea :)


+public:
+  template <typename V> struct result_pair {
+    result_pair(std::size_t Index, V Value) : Index(Index), Value(Value) {}
+

Why not a std::pair?

+    iterator &operator=(const iterator &RHS) {
+      Iter = RHS;
+      return *this;
+    }
+

What about the Index here?



— 
Mehdi



More information about the llvm-commits mailing list