Shout out to D at cppcon, when talkign about ranges.
bitwise via Digitalmars-d
digitalmars-d at puremagic.com
Wed Sep 30 18:32:15 PDT 2015
On Thursday, 1 October 2015 at 00:42:43 UTC, lobo wrote:
> If ranges are accepted into ISO C++ I can't imagine it would be
> long before for(auto e:range).
Special features are not necessary to do this. C++ for loop works
on anything with begin()/end() functions. Real ranges could just
be a pair of iterators, and lazy ranges could give out dummies:
template<class T>
struct iota
{
T first, last;
iota(T first, T last)
: first(first), last(last) {}
T front() const { return first; }
void popFront() { ++first; }
bool empty() const { return first == last; }
struct iterator {
iota *_iota;
bool operator!=(const iterator& that) const {
assert(_iota == that._iota);
return !_iota->empty();
}
void operator++() {
_iota->popFront();
}
T operator*() const {
return _iota->front();
}
};
iterator begin() { return iterator{this}; }
iterator end() { return iterator{this}; }
};
int main(int argc, const char * argv[])
{
for(int x : iota<int>(0, 5))
printf("%d ", x);
return 0;
}
I don't think it would be that hard to make something this
possible:
for(int x : iota<int>(0, 5).to<take>(3))
printf("%d ", x);
The iterator dummy could probably be generalized with a template.
Bit
More information about the Digitalmars-d
mailing list