My recommendation is that you use iterators, I wrote an example below. Something like this would do it in C++ < C++17:
#include <iostream>
template <class T>
struct RangeIter {
RangeIter(T from, T to, T curr ) :
_from(from), _to(to), _curr(curr) {
}
T operator*() const {
return _curr;
}
T operator++() {
++_curr;
return _curr;
}
bool operator==(const RangeIter & other) {
assert(_from == other._from && _to == other._to);
return _curr == other._curr;
}
bool operator!=(const RangeIter & other) {
return !(_curr == other._curr);
}
T _from, _to, _curr;
};
template <class T>
struct Range {
Range(T from, T to) : _from(from), _to(to) {}
RangeIter<T> begin() { return RangeIter<T>(_from, _to, _from); }
RangeIter<T> end() {
return RangeIter<T>(_from, _to, _to);
}
T _from, _to;
};
template <class T>
Range<T> makeRange(T to, T from) {
return Range<T>(to, from);
}
int main() {
for (auto i : makeRange(0, 10)) {
std::cout << i << std::endl;
}
}
For C++17 you can use different types for the begin and end iterator and improve on this. You can use sentinels. You could take a look here: How the new range-based for loop in C++17 helps Ranges TS?
A C++-17 only solution here:
#include <iostream>
template <class T>
struct RangeSentinel {
RangeSentinel(T stopVal) : _stopVal(stopVal) {}
T _stopVal;
};
template <class T>
struct RangeIter {
RangeIter(T from, T to, T curr) :
_from(from), _to(to), _curr(curr) {
}
T operator*() const {
return _curr;
}
T operator++() {
++_curr;
return _curr;
}
bool operator==(const RangeSentinel<T> & other) {
assert(_from == other._from && _to == other._to);
return _curr == other._stopVal;
}
bool operator!=(const RangeSentinel<T> & other) {
return !(_curr == other._stopVal);
}
T _from, _to, _curr;
};
template <class T>
struct Range {
Range(T from, T to) : _from(from), _to(to) {}
RangeIter<T> begin() { return RangeIter<T>(_from, _to, _from); }
RangeSentinel<T> end() {
return RangeSentinel<T>(_to);
}
T _from, _to;
};
template <class T>
Range<T> makeRange(T to, T from) {
return Range<T>(to, from);
}
int main() {
for (auto i : makeRange(0, 10)) {
std::cout << i << std::endl;
}
}
As you can see, in the C++17 solution I do not need to store again _from and _to variables, since the sentinel is a different type.