
Eric Niebler wrote:
Anatoli Tubman wrote:
John Torjo wrote:
However, had I used crange<int> r(d); // how do I know what iterators to keep inside? crange<int> r(v); // how do I know what iterators to keep inside?
You keep a pointer to crange_impl_base<int> which points to a heap-allocated instance of crange_impl<int, d_array>
Nope. :-)
Another option is to write
for (crange<int>& r = mkrange(d); r; ++r) { ... }
where mkrange returns crange_impl<int, d_array> which is derived from crange<int>.
This doesn't need heap allocation, but still needs virtual operator++ and virtual operator*.
Not that either. :-) With the FOREACH macro, you don't have to specify the container type, there is no heap allocation and no virtual function calls. Everything is fully-inline-able. And it doesn't need typeof.
Your technique is indeed very cool and useful, when you want to only go forward ;) The problem is if you need "custom incrementing". Like, sometimes you might want to go back, or twice forward and such. Or, when you need two consecutive values, like *r and *(r-1) for ( crange<container> r(cont); r; ++r) { if ( *r == 10) { *(--r) = 0; ++r; } } Best, John