Skip to content

Instantly share code, notes, and snippets.

@elbeno
Created November 22, 2016 17:27
Show Gist options
  • Save elbeno/cacc9701e5d000376735ed596c414527 to your computer and use it in GitHub Desktop.
Save elbeno/cacc9701e5d000376735ed596c414527 to your computer and use it in GitHub Desktop.
Integral ranges with initializer_lists and parameter packs
#include <cstddef>
#include <iostream>
#include <type_traits>
#include <utility>
using namespace std;
template <typename T, T Start, T... Ts>
const auto& make_int_range_helper(std::integer_sequence<T, Ts...>) {
static const initializer_list<int> il{ (Start+Ts)...};
return il;
}
template <typename T = size_t, T Start = 0, T End,
typename = std::enable_if_t<std::is_integral<T>::value>>
decltype(auto) make_int_range() {
return make_int_range_helper<T, Start>(std::make_integer_sequence<T, End-Start>());
}
template <size_t N>
decltype(auto) make_index_range() {
return make_int_range<size_t, 0, N>();
}
int main() {
for (auto i : make_index_range<10>()) {
cout << i << '\n';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment