On Thursday, 1 October 2015 at 05:47:25 UTC, Eric Niebler wrote:
On Thursday, 1 October 2015 at 04:08:00 UTC, bitwise wrote:
I understand, but the C++ committee seems very conservative to
me, so when it's this easy to add for(:) support by giving
ranges begin()/end() functions, it makes me doubt they will
actually change the language for it.
As of C++11, C++ has the for(auto e:range) control structure
you are looking for. I would be using it here except for one
thing: in my proposal, begin() and end() don't have to return
objects of the same type! begin() must return an iterator and
end() must return something that is EqualityComparable with the
iterator -- but it doesn't have to be an iterator. That makes
many types of iterators vastly simpler to implement and more
efficient at runtime.
C++'s built-in range-based for(:) loop expects begin() and
end() to return objects of the same type. The committee is
already talking about loosening that constraint so that the
ranges I'm proposing Just Work with the existing built-in
looping construct. Until then, there is an ugly macro. It's a
temporary hack, nothing more.
Did you look at my example(this one is updated)?
http://ideone.com/X1JAEn
I've factored out a 'range_iterator' in this version. I would
probably rethink the way I've done the chaining, but there is
nothing complicated about adding foreach support as I've
implemented it in my example. From the callers point of view,
it's a typedef and two functions.
I really doubt anything could be done that would offer a
perceptible performance gain over what I've done in my example.
I suppose it's a moot point if C++ is actually planning native
range support.
Bit