In modern programming sometimes we want to use a sequence of integers that are created at compile-time. In C++14, the std::integer_sequence
is a class template for the sequence of integers that is generated at compile-time. In this post, we explain what integer sequence (std::integer_sequence
) is in modern programming.
What is the integer sequence (std::integer_sequence) in C++ 14?
In C++14, the std::integer_sequence is a class template defined in a header that can be used for the sequence of integers generated at compile-time. In some cases, looping through a range of numbers whose span is unknown is used and, in these cases, we can use the std::integer_sequence
integer sequence. Thus, we can create a sequence of integers at compile time. Our application knows the sequence of integers before it runs, and we use them on runtime as a package. In other words, the std::integer_sequence
is used to hold a sequence of integers which can be turned into a parameter pack. We can use integer_sequence
in template programming or meta-programming algorithms, and this will make our code faster and less complex.
In C++14, a simple syntax for the std::integer_sequence
can be written as shown below.
1 2 3 |
template< class T, T... Ints > class integer_sequence; |
Here, T
is the type of integers and val
is a parameter pack of integers.
Is there a simple integer sequence (std::integer_sequence) example in C++ 14?
Here is a simple example how we can use std::integer_sequence
.
1 2 3 4 5 6 |
template <typename T, T... val> void print_sequence2(std::integer_sequence<T, val...>) { ( (std::cout << val << ','), ...); } |
Here is another example with std::initializer_list
.
1 2 3 4 5 6 7 |
template <typename T, T... val> void print_sequence(std::integer_sequence<T, val...>) { std::initializer_list<bool>{ bool(std::cout << val << ',')... }; std::cout << std::endl; } |
Is there a full integer sequence (std::integer_sequence) example in C++ 14?
Here is a full integer sequence (std::integer_sequence) example in C++14 and C++17.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> #include <utility> template <typename T, T... val> void print_sequence(std::integer_sequence<T, val...>) { std::initializer_list<bool>{ bool(std::cout << val << ',')... }; std::cout << std::endl; } template <typename T, T... val> void print_sequence2(std::integer_sequence<T, val...>) { ( (std::cout << val << ','), ...); } int main() { using mysequence = std::integer_sequence<int, 9, 8, 7, 6, 5>; print_sequence( mysequence() ); print_sequence(std::integer_sequence<int, 1, 2, 3>()); print_sequence2( mysequence() ); print_sequence2(std::integer_sequence<int, 1, 2, 3>()); system("pause"); return 0; } |
For more information, please see https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3658.html
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition