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

Lang Hames lhames at gmail.com
Wed Mar 19 11:13:32 PDT 2014


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(); }

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140319/99da0e4c/attachment.html>


More information about the llvm-dev mailing list