Add llvm::enumerate() to stl extras
Mehdi Amini via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 29 12:09:00 PDT 2016
Make sense!
Can you also add doxygen to the new API?
—
Mehdi
> On Sep 29, 2016, at 12:06 PM, Zachary Turner <zturner at google.com> wrote:
>
> 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 <mailto:mehdi.amini at apple.com>> wrote:
>
> > On Sep 29, 2016, at 11:23 AM, Zachary Turner <zturner at google.com <mailto: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/b3d20909/attachment.html>
More information about the llvm-commits
mailing list