<div dir="ltr">Hi all,<div><br></div><div>As RFCs go this is short and sweet - I think it would be nice to add an empty method to iterator_range. Something like:</div><div><br></div><div>bool empty() const { return begin() != end(); }</div>
<div><br></div><div>My motivation is to make the 'if' test at the start of this common pattern tidier:</div><div><br></div><div>if (!collection.empty())</div><div> // initialization...</div><div>for (auto& c : collection)</div>
<div> // do something for each c.</div><div><br></div><div>which I think that is preferable to:</div><div><br></div><div>if (collection.begin() != collection.end())</div><div> // initialization...</div><div>// for each...</div>
<div><br></div><div>The empty method is just a single iterator comparison, so it should be valid and cheap for any underlying iterator type.</div><div><br></div><div>Pros:</div><div>- Enables small aesthetic improvements.</div>
<div>- 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.</div>
<div><br></div><div>Cons: ? </div><div><br></div><div>Cheers,</div><div>Lang.</div></div>