[LLVMdev] [RFC] Add empty() method to iterator_range.

Roel Jordans r.jordans at tue.nl
Thu Mar 20 02:20:45 PDT 2014



On 19/03/14 19:13, Lang Hames wrote:
> Hi all,
>
> As RFCs go this is short and sweet - I think it would be nice to add an
> empty method to iterator_range. Something like:
>
> bool empty() const { return begin() != end(); }
>

I think this should be

bool empty() const { return begin() == end(); }

Otherwise, seems useful to me!

- Roel

> My motivation is to make the 'if' test at the start of this common
> pattern tidier:
>
> if (!collection.empty())
>    // initialization...
> for (auto& c : collection)
>    // do something for each c.
>
> which I think that is preferable to:
>
> if (collection.begin() != collection.end())
>    // initialization...
> // for each...
>
> The empty method is just a single iterator comparison, so it should be
> valid and cheap for any underlying iterator type.
>
> Pros:
> - Enables small aesthetic improvements.
> - Would make iterator_range look slightly more "container-like", so it
> could substitute into more template code, though I'd expect templates
> that take read-only containers rather than iterators to be very rare in
> C++, and templates that take collections and use only begin, end and
> empty to be rarer still.
>
> Cons: ?
>
> Cheers,
> Lang.
>
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>



More information about the llvm-dev mailing list