Created
May 16, 2020 17:34
-
-
Save vittorioromeo/50a9b509f4e563c668dff0a0d840791e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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