std:: integer_sequence
|
헤더에 정의됨
<utility>
|
||
|
template
<
class
T, T...
Ints
>
class integer_sequence ; |
(C++14부터) | |
클래스 템플릿
std::integer_sequence
는 컴파일 타임 정수 시퀀스를 나타냅니다.
함수 템플릿
의 인자로 사용될 때,
매개변수 팩
Ints
는 추론되어 팩 확장에서 사용될 수 있습니다.
목차 |
템플릿 매개변수
| T | - | 시퀀스의 요소에 사용할 정수 타입 |
| ...Ints | - | 시퀀스를 나타내는 상수 매개변수 팩 |
멤버 타입
| 타입 | 정의 |
value_type
|
T
|
멤버 함수
|
size
[static]
|
Ints
의 요소 개수를 반환합니다
(public static member function) |
std::integer_sequence:: size
|
static
constexpr
std::
size_t
size
(
)
noexcept
;
|
||
Ints
의 요소 개수를 반환합니다.
sizeof...
(
Ints
)
와 동일합니다.
반환 값
Ints
의 요소 개수입니다.
헬퍼 템플릿
보조 별칭 템플릿
std::index_sequence
은
T
가
std::size_t
인 일반적인 경우를 위해 정의됩니다:
|
template
<
std::
size_t
...
Ints
>
using index_sequence = std :: integer_sequence < std:: size_t , Ints... > ; |
||
헬퍼 별칭 템플릿
std::make_integer_sequence
과
std::make_index_sequence
는 각각
std::integer_sequence
와
std::index_sequence
타입 생성을 단순화하기 위해 정의되며,
0
,
1
,
2
,
...
,
N
-
1
을
Ints
로 갖습니다:
|
template
<
class
T, T N
>
using make_integer_sequence = std :: integer_sequence < T, /* 0, 1, 2, ..., N-1 시퀀스 */ > ; |
||
|
template
<
std::
size_t
N
>
using make_index_sequence = std :: make_integer_sequence < std:: size_t , N > ; |
||
프로그램은
N
이 음수일 경우 형식이 잘못되었습니다.
N
이 0인 경우, 지정된 타입은
integer_sequence<T>
입니다.
도우미 별칭 템플릿
std::index_sequence_for
은 모든 타입 매개변수 팩을 동일한 길이의 인덱스 시퀀스로 변환하기 위해 정의됩니다:
|
template
<
class
...
T
>
using index_sequence_for = std :: make_index_sequence < sizeof... ( T ) > ; |
||
참고 사항
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_integer_sequence
|
201304L
|
(C++14) | 컴파일 타임 정수 시퀀스 |
가능한 구현
| make_integer_sequence |
|---|
namespace detail { template<class T, T I, T N, T... integers> struct make_integer_sequence_helper { using type = typename make_integer_sequence_helper<T, I + 1, N, integers..., I>::type; }; template<class T, T N, T... integers> struct make_integer_sequence_helper<T, N, N, integers...> { using type = std::integer_sequence<T, integers...>; }; } template<class T, T N> using make_integer_sequence = detail::make_integer_sequence_helper<T, 0, N>::type; |
예제
참고 항목 std::apply 다른 예제에 대한 가능한 구현.
#include <array> #include <cstddef> #include <iostream> #include <tuple> #include <utility> namespace details { template <typename Array, std::size_t... I> constexpr auto array_to_tuple_impl(const Array& a, std::index_sequence<I...>) { return std::make_tuple(a[I]...); } template <class Ch, class Tr, class Tuple, std::size_t... Is> void print_tuple_impl(std::basic_ostream<Ch, Tr>& os, const Tuple& t, std::index_sequence<Is...>) { ((os << (Is ? ", " : "") << std::get<Is>(t)), ...); } } template <typename T, T... ints> void print_sequence(int id, std::integer_sequence<T, ints...> int_seq) { std::cout << id << ") 크기가 " << int_seq.size() << ": "; ((std::cout << ints << ' '), ...); std::cout << '\n'; } template <typename T, std::size_t N, typename Indx = std::make_index_sequence<N>> constexpr auto array_to_tuple(const std::array<T, N>& a) { return details::array_to_tuple_impl(a, Indx{}); } template <class Ch, class Tr, class... Args> auto& operator<<(std::basic_ostream<Ch, Tr>& os, const std::tuple<Args...>& t) { os << '('; details::print_tuple_impl(os, t, std::index_sequence_for<Args...>{}); return os << ')'; } int main() { print_sequence(1, std::integer_sequence<unsigned, 9, 2, 5, 1, 9, 1, 6>{}); print_sequence(2, std::make_integer_sequence<int, 12>{}); print_sequence(3, std::make_index_sequence<10>{}); print_sequence(4, std::index_sequence_for<std::ios, float, signed>{}); constexpr std::array<int, 4> array{1, 2, 3, 4}; auto tuple1 = array_to_tuple(array); static_assert(std::is_same_v<decltype(tuple1), std::tuple<int, int, int, int>>, ""); std::cout << "5) tuple1: " << tuple1 << '\n'; constexpr auto tuple2 = array_to_tuple<int, 4, std::integer_sequence<std::size_t, 1, 0, 3, 2>>(array); std::cout << "6) tuple2: " << tuple2 << '\n'; }
출력:
1) 크기 7의 시퀀스: 9 2 5 1 9 1 6 2) 크기 12의 시퀀스: 0 1 2 3 4 5 6 7 8 9 10 11 3) 크기 10의 시퀀스: 0 1 2 3 4 5 6 7 8 9 4) 크기 3의 시퀀스: 0 1 2 5) tuple1: (1, 2, 3, 4) 6) tuple2: (2, 1, 4, 3)
참고 항목
|
(C++20)
|
내장 배열로부터
std::array
객체를 생성합니다
(함수 템플릿) |
|
(C++11)
(C++17)
|
지정된 타입과 값을 가진 컴파일 타임 상수
(클래스 템플릿) |