Run Code
|
Code Wall
|
Users
|
Misc
|
Feedback
|
About
|
Login
|
Theme
|
Privacy
Variadic Template: Make Index Sequence
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#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; }
clang++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.75 sec, absolute running time: 0.07 sec, cpu time: 0.02 sec, memory peak: 3 Mb, absolute service time: 0,83 sec
edit mode
|
history
|
discussion
[5] 6 5 4 3 2 [5] 2 3 4 5 6 [9] -6 -5 -4 -3 -2 -1 0 1 2 [9] -2 -1 0 1 2 3 4 5 6