How to access n-th value of an integer_sequence?

  • A+

I would like to know how to access the n-th value of an std::integer_sequence. For example given a type

using foo = std::integer_sequence<int, 3, 1, 4>; 

I would like to have something like

auto i = get<foo, 2>(); // i = 4 

Is there something in the standard library to do that? If not, do I need to resort to an iterative solution if I want this to work in C++14 (not C++17) ?


There is no such built-in method as far as I'm aware but you can implement it itself in a few neat lines without any iterations:

template<class T, T... Ints> constexpr T get(std::integer_sequence<T, Ints...>, std::size_t i) {     constexpr T arr[] = {Ints...};     return arr[i]; } 

See how it works here:

Arguments can be lifted into template parameters (to match your example) with a bit more code.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: