[llvm-dev] arg_iterator missing inc/dec operators

David Blaikie via llvm-dev llvm-dev at lists.llvm.org
Thu Mar 23 08:10:14 PDT 2017


On Wed, Mar 22, 2017 at 1:23 PM Dmitry Babokin via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> Reid,
>
> std::prev() works, though IMHO increment/decrement are more conventional
> way (I'm not saying better) to work with iterators.
>

To be clear, increment/decrement do work with pointers, but the specific
case here is that they don't work with rvalue pointers. So this works:

iterator I = func();
--I;

but this does not:

  --func()

^ is not guaranteed to work with all iterators (pointers being the obvious
example - which would be true in a simple implementation of std::vector).
It only happens to work with non-trivial iterators, where
increment/decrement are implemented by operator overloads, rather than
naturally by a pointer.

That's why std::next/prev exist - to help with this case when writing
portable/general code.

Actually, I suppose we could defensively disable this for non-trivial
iterators by deleting rvalue overloads of the increment/decrement
operators...


>
> I have no problem fixing code on my side, I'm just bringing your attention
> that code like this is broken. If your judgement is to keep it the way it
> is now, I'm ok with it.
>
> Thanks.
>
> Dmitry.
>
> On Wed, Mar 22, 2017 at 1:20 PM, Reid Kleckner <rnk at google.com> wrote:
>
> On Wed, Mar 22, 2017 at 1:12 PM, Dmitry Babokin <babokin at gmail.com> wrote:
>
> Correct, I mean exactly this. Though &* is not important in this case.
>
> This code doesn't work anymore, as arg_iterator is defined differently
> now:
> https://github.com/llvm-mirror/llvm/blob/master/include/llvm/IR/Function.h#L57
>
> I get an error when I'm using pre-decrement operator:
> error: expression is not assignable
>       --f->arg_end();
>
>
> Today's arg_iterator is now just an Argument*, which is the world's
> simplest iterator. The '--f->arg_end()' snippet only compiled before this
> change because we used to use fancy ilist iterators which had overloaded
> decrement operators.
>
> I don't think we should make a wrapper pointer iterator type just to make
> old code like this compile.
>
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170323/e2e739e2/attachment.html>


More information about the llvm-dev mailing list