Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: Lang Hames <lhames <at> gmail.com>
Subject: [RFC] Add empty() method to iterator_range.
Newsgroups: gmane.comp.compilers.llvm.devel
Date: Wednesday 19th March 2014 18:13:32 UTC (over 3 years ago)
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.
 
CD: 4ms