I found a forum post about someone wanting to easy iterating over a range of numbers -> normal for loops are too heavy, too error prune...
The answer from one guy was very interesting, so i want to share that with you:
This is the code he put up there:
namespace util {
class range {
public:
class iterator {
friend class range;
public:
long int operator *() const {
return i_;
}
const iterator &operator ++() {
++i_; return *this;
}
iterator operator ++(int) {
iterator copy(*this); ++i_; return copy;
}
bool operator ==(const iterator &other) const {
return i_ == other.i_;
}
bool operator !=(const iterator &other) const {
return i_ != other.i_;
}
protected:
iterator(long int start) : i_(start) {
}
private:
unsigned long i_;
};
iterator begin() const {
return begin_;
}
iterator end() const {
return end_;
}
range(long int begin, long int end) : begin_(begin), end_(end) {
}
private:
iterator begin_;
iterator end_;
};
};
So that you can easiely iterate over a range of numbers with a crap api:
auto range = util::range(firstIndex, lastIndex + 1);
for (auto i : range) {
std::cout << i << std::endl;
}
I have no words for this, just compare it with a simple for loop doing the same thing.
Without any compiler optimization, its ridiculous how much instructions you pay for abstraction like this.
Crap like this is the reason why almost every software on this planet is slow - especially the web!
This is just a simple for loop, imaging that more complex stuff are build like this...
What do you think?