Commit 14ac59f9 authored by Kenton Varda's avatar Kenton Varda

Add kj::zeroTo(x), equivalent to kj::range(0, x).

The problem with kj::range() is that if the inputs are not recognized as the same type, the typechecker can get annoyed.
parent 2a57a2aa
......@@ -643,6 +643,7 @@ template <typename T>
class Range {
public:
inline constexpr Range(const T& begin, const T& end): begin_(begin), end_(end) {}
inline explicit constexpr Range(const T& end): begin_(0), end_(end) {}
class Iterator {
public:
......@@ -690,6 +691,14 @@ inline constexpr Range<Decay<T>> range(T begin, T end) { return Range<Decay<T>>(
// // Prints 1, 2, 3, 4, 5, 6, 7, 8, 9.
// for (int i: kj::range(1, 10)) { print(i); }
template <typename T>
inline constexpr Range<Decay<T>> zeroTo(T end) { return Range<Decay<T>>(end); }
// Returns a fake iterable container containing all values of T from zero (inclusive) to `end`
// (exclusive). Example:
//
// // Prints 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
// for (int i: kj::zeroTo(10)) { print(i); }
template <typename T>
inline constexpr Range<size_t> indices(T&& container) {
// Shortcut for iterating over the indices of a container:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment