Created
November 22, 2016 17:27
-
-
Save elbeno/cacc9701e5d000376735ed596c414527 to your computer and use it in GitHub Desktop.
Integral ranges with initializer_lists and parameter packs
This file contains hidden or 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
#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