tuple_size

tuple_size (튜플 크기)

std::tuple_size는 튜플의 요소 개수를 컴파일 타임 상수 표현식으로 제공하는 클래스 템플릿이에요. 이 페이지에서는 std::tuple에 대한 특수화와 C++17부터 제공되는 헬퍼 변수 템플릿 tuple_size_v를 다루고 있어요. C++11부터 사용할 수 있어요.

출처: cppreference

본문

<tuple> 헤더에 정의되어 있어요.

정의
template < class ... Types > struct tuple_size < std :: tuple < Types ... > > : std :: integral_constant < std :: size_t , sizeof ...( Types ) > { }; (C++11부터)

이 특수화는 튜플의 요소 개수를 컴파일 타임 상수 표현식으로 접근할 수 있게 해줘요.

헬퍼 변수 템플릿

정의
template < class T > constexpr std :: size_t tuple_size_v = tuple_size < T >:: value ; (C++17부터)

std::integral_constant에서 상속받은 멤버

멤버 상수

이름 설명
value [static] sizeof...(Types) (공용 정적 멤버 상수)

멤버 함수

이름 설명
operator std::size_t 객체를 std::size_t로 변환하고 value를 반환해요 (공용 멤버 함수)
operator() (C++14) value를 반환해요 (공용 멤버 함수)

멤버 타입

타입 정의
value_type std::size_t
type std::integral_constant<std::size_t, value>

예제

#include <iostream>
#include <tuple>

template <class T>
void test(T value)
{
    int a[std::tuple_size_v<T>]; // can be used at compile time

    std::cout << std::tuple_size<T>{} << ' ' // or at run time
              << sizeof a << ' '
              << sizeof value << '\n';
}

int main()
{
    test(std::make_tuple(1, 2, 3.14));
}

가능한 출력:

3 12 16

같이 보기

항목 설명
Structured binding (C++17) 지정된 이름을 초기화자의 하위 객체나 튜플 요소에 바인딩해요 [edit]
tuple_size (C++11) 튜플 유사 타입의 요소 개수를 얻어요 (클래스 템플릿) [edit]
tuple_size (C++26) 반영된 튜플 유사 타입의 크기를 반환해요 (함수) [edit]
std::tuple_size<std::pair> (C++11) pair의 크기를 얻어요 (클래스 템플릿 특수화) [edit]
std::tuple_size<std::array> (C++11) array의 크기를 얻어요 (클래스 템플릿 특수화) [edit]
std::tuple_size<std::ranges::subrange> (C++20) std::ranges::subrange의 크기를 얻어요 (클래스 템플릿 특수화) [edit]
std::tuple_size<std::complex> (C++26) std::complex의 크기를 얻어요 (클래스 템플릿 특수화) [edit]
std::tuple_element<std::tuple> (C++11) 지정된 요소의 타입을 얻어요 (클래스 템플릿 특수화) [edit]
get (std::tuple) (C++11) 튜플의 지정된 요소에 접근해요 (함수 템플릿) [edit]

더 알아보기 (Learn more)

cppreference