Add llvm::enumerate() to stl extras

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 29 12:06:18 PDT 2016


I had a pair originally, but I found it more intuitive to refer to them in
loops with the names "Index" and "Value" rather than "first" and "second".

On Thu, Sep 29, 2016 at 12:05 PM Mehdi Amini <mehdi.amini at apple.com> wrote:

>
> > 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160929/0e6e571b/attachment.html>


More information about the llvm-commits mailing list