Basically, I’m a bit tired of writing:
std::map<key_t, val_t> the_map;
...
auto iterator = the_map.find(...);
if(iterator != the_map.end()) { // note the "inversed" logic and logically superflous end() call
...
}
What really made sense would be:
if(auto x=the_map.find(...)) {
... // x could either be an iterator or maybe something like boost::optional<val_t>
}
Is there some prior art that defines some helper stuff to shorten the != container.end() syntax or am I the only one annoyed by this?
You could write a class template
auto_iterator_impland use it through a function templateauto_iteratorwhich returns an instance ofauto_iterator_impl, which can be implicitly converted intotrueorfalse:A working implementation with minimal functionalities and consideration:
Test code:
Output:
Online demo : http://www.ideone.com/MnISh