Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Variadic Template: Make Index Sequence
#include <iostream> #include <type_traits> using SizeT = int; template <SizeT...> struct index_sequence {}; /////////////////////////////// template <SizeT Step> struct Helper { template <SizeT Count, SizeT Limit, SizeT ... Z> struct index_sequence_helper { using type = typename index_sequence_helper<Count-1, Limit-Step, Limit, Z...>::type; }; template <SizeT A, SizeT ... Z> struct index_sequence_helper<1, A, Z...> { using type = index_sequence<A, Z...>; }; }; /////////////////////////////// template <SizeT From, SizeT To> constexpr bool FromMoreThanTo() { return (From > To); } template <SizeT From, SizeT To> constexpr auto make(std::enable_if_t<FromMoreThanTo<From, To>(), bool> = true) { return typename Helper<-1>::template index_sequence_helper<(From-To)+1, To>::type(); } template <SizeT From, SizeT To> constexpr auto make(std::enable_if_t<!FromMoreThanTo<From, To>(), bool> = true) { return typename Helper<1>::template index_sequence_helper<(To-From)+1, To>::type(); } /////////////////////////////// template <SizeT ... S> void print(index_sequence<S...>) { SizeT val[] = {S...} ; std::cout << "[" << sizeof...(S) << "]\t"; for (auto v : val) std::cout << v << ' '; std::cout << "\n"; } int main() { print(make<6, 2>()); print(make<2, 6>()); print(make<-6, 2>()); print(make<-2, 6>()); return 0; }
run
|
edit
|
history
|
help
0
Balanced Insert Heap Example
Magic, why 1 2?
STL stack
Assertion Divide by Zero Example
C++ Operator Overloading
Enum flags operator example
MPL 2-0
C++ Standard Template Library
IsContainer
std::99 bottles of beer!