Skip to content

Instantly share code, notes, and snippets.

@vittorioromeo
Created May 16, 2020 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vittorioromeo/50a9b509f4e563c668dff0a0d840791e to your computer and use it in GitHub Desktop.
Save vittorioromeo/50a9b509f4e563c668dff0a0d840791e to your computer and use it in GitHub Desktop.
template <typename T>
struct range {
T _a;
T _b;
[[nodiscard]] constexpr explicit range(T a, T b) noexcept : _a{a}, _b{b} {}
struct end_sentinel {
T _value;
};
struct iter {
T _value;
[[nodiscard]] constexpr bool operator!=(const end_sentinel& e) noexcept {
return _value != e._value;
}
constexpr void operator++() noexcept { ++_value; }
[[nodiscard]] constexpr T operator*() const noexcept { return _value; }
};
[[nodiscard]] constexpr iter begin() const noexcept { return {_a}; }
[[nodiscard]] constexpr end_sentinel end() const noexcept { return {_b}; }
};
int main() {
int ctr = 0;
for (const auto i : range{0, 100}) {
ctr += i;
}
return ctr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment