Open In App

std::integer_sequence in C++ 14

Last Updated : 29 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

When programming, loops, and iteration are often required by developers. Occasionally, looping through a range of numbers whose span is unknown becomes necessary std::integer sequence is useful in that scenario.

By using std::integer_sequence provided by C++14, one can create a sequence of integers at the compile time. The program knows the sequence of integers before it runs. With this tool, one can engage in template programming, and meta-programming, and make coding less complex.

The parameter pack Ints which represents the sequence of elements can be deduced and used in pack expansion when std::integer_sequence is used as an argument to a function template.

Syntax

template <typename T, T... Ints>
struct integer_sequence;

Where struct is used to define an integer sequence with the given values in Ints.

Template Parameters

  • T: It is the type of the integers.
  • Ints: It is a parameter pack of integers.

Member functions

  • size(): It returns the number of elements in the sequence of elements represented by parameter pack Ints.

Examples of std::integer_sequence

Example 1:

In the below code, we will make use of the above syntax to demonstrate the use of the std::integer_sequence in C++ 14.

C++




#include <iostream>
#include <utility>
using namespace std;
  
template <typename T, T... Is>
void print_sequence(integer_sequence<T, Is...>)
{
    cout << "The sequence is: ";
    ((cout << Is << ' '),
     ...); // fold expression to print sequence
}
  
int main()
{
    print_sequence(integer_sequence<int, 1, 2, 3, 4>{});
    return 0;
}


Output

The sequence is: 1 2 3 4

Explanation:

In this code, A std::integer sequence parameter is passed to the print_sequence function, which is defined in this code. The function then prints its values. Printing the sequence on a single line separated by spaces is done by utilizing a fold expression.

Example 2:

In the below code, the integer_sequence_size struct uses template specialization to recursively calculate the size of the integer_sequence.

C++




#include <iostream>
#include <utility>
using namespace std;
  
// Struct to calculate the size of an integer_sequence
template <typename T, T... Ints>
struct integer_sequence_size;
  
// Partial specialization for an integer_sequence with at
// least one element
template <typename T, T Head, T... Tail>
struct integer_sequence_size<T, Head, Tail...> {
    static constexpr size_t value
        = 1 + integer_sequence_size<T, Tail...>::value;
};
  
// Specialization for an empty integer_sequence
template <typename T> struct integer_sequence_size<T> {
    static constexpr size_t value = 0;
};
  
int main()
{
    // Define an integer_sequence
    using my_sequence
        = integer_sequence<int, 0, 1, 2, 3, 4>;
  
    // Print the size of the integer_sequence using the
    // custom struct
    std::cout
        << "Size: "
        << integer_sequence_size<int, 0, 1, 2, 3, 4>::value
        << endl;
    // Output: Size: 5
  
    return 0;
}


Output

Size: 5

Advantages of using std::integer_sequence in C++ 14 are:

  • Simplifies template metaprogramming: By presenting a straightforward approach for producing compile-time sequences of integers, std::integer sequence streamlines template metaprogramming in C++14. It reduces the necessity of tangled and inaccurate recursive template functions.
  • Improves readability: By using std::integer sequence, you can write more simple code, since it removes the requirement for complicated template function calls.
  • Enables pack expansion: Std::integer sequence enables the creation of efficient code by allowing for the expansion of parameter packs. Furthermore, it has the capability to process numerous template arguments.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads