Namespaces
Variants

std:: tuple_size

From cppreference.net
Utilities library
헤더에 정의됨 <array>
헤더에 정의됨 <tuple>
헤더에 정의됨 <utility>
헤더에 정의됨 <ranges>
(C++20부터)
헤더에 정의됨 <complex>
(C++26부터)
template < class T >
struct tuple_size ; // 정의되지 않음
(1) (C++11부터)
template < class T >

struct tuple_size < const T >

: std:: integral_constant < std:: size_t , std :: tuple_size < T > :: value > { } ;
(2) (C++11부터)
template < class T >

struct tuple_size < volatile T >

: std:: integral_constant < std:: size_t , std :: tuple_size < T > :: value > { } ;
(3) (C++11부터)
(C++20부터 사용 중단됨)
template < class T >

struct tuple_size < const volatile T >

: std:: integral_constant < std:: size_t , std :: tuple_size < T > :: value > { } ;
(4) (C++11부터)
(C++20부터 사용 중단됨)

튜플-라이크 타입의 요소 개수에 컴파일 타임 상수 표현식으로 접근할 수 있도록 합니다.

1) 기본 템플릿은 정의되지 않았습니다. 타입을 튜플처럼 만들기 위해서는 명시적(전체) 또는 부분 특수화가 필요합니다.
2-4) cv 한정 타입에 대한 특수화는 기본적으로 해당 cv 비한정 버전의 value 를 재사용합니다.

std::tuple_size 는 핵심 언어와 상호작용합니다: 튜플-유사(tuple-like) 경우에 구조적 바인딩 지원을 제공할 수 있습니다.

(2-4) 는 SFINAE-친화적입니다: 만약 std :: tuple_size < T > :: value 가 평가되지 않은 피연산자로 취급될 때 형식이 잘못된 경우, 이들은 멤버 value 를 제공하지 않습니다. 접근 검사는 tuple_size T 와 무관한 컨텍스트에서 수행된 것처럼 처리됩니다. 표현식의 직접적인 컨텍스트의 유효성만 고려됩니다. 이는 다음을 허용합니다

#include <utility>
struct X { int a, b; };
const auto [x, y] = X(); // 구조적 바인딩 선언은 먼저 tuple_size<const X>를 시도하며,
                         // 이는 tuple_size<X>::value 사용을 시도하고,
                         // 이후 소프트 에러가 발생하여 공개 데이터 멤버에 바인딩합니다
(C++17부터)

목차

특수화

표준 라이브러리는 표준 라이브러리 타입에 대해 다음과 같은 특수화를 제공합니다:

tuple 의 크기를 구함


(클래스 템플릿 특수화)

pair 의 크기를 구함
(클래스 템플릿 특수화)
array 의 크기를 구함
(클래스 템플릿 특수화)
std::ranges::subrange 의 크기를 구함
(클래스 템플릿 특수화)
std::complex 의 크기를 구함
(클래스 템플릿 특수화)

std::tuple_size 의 모든 특수화는 UnaryTypeTrait 요구 사항을 만족하며, 기본 특성 std:: integral_constant < std:: size_t , N > 형태를 가집니다(여기서 N 은 임의의 값).

사용자는 프로그램 정의 타입에 대해 std::tuple_size 를 특수화하여 튜플처럼 만들 수 있습니다. 프로그램 정의 특수화는 위의 요구사항을 충족해야 합니다.

일반적으로 cv-unqualified 타입에 대한 특수화만 사용자 정의할 필요가 있습니다.

헬퍼 변수 템플릿

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

std:: integral_constant 에서 상속됨

멤버 상수

value
[static]
표준 특수화의 경우, 튜플과 유사한 타입 T 의 요소 개수
(public static member constant)

멤버 함수

operator std::size_t
객체를 std:: size_t 로 변환하며, value 를 반환함
(public member function)
operator()
(C++14)
value 를 반환함
(public member function)

멤버 타입

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

예제

#include <array>
#include <cstddef>
#include <ranges>
#include <tuple>
#include <utility>
template<class T, std::size_t Size> struct Arr { T data[Size]; };
// std::tuple_size의 프로그램 정의 특수화:
template<class T, std::size_t Size> struct std::tuple_size<Arr<T, Size>>
    : public integral_constant<std::size_t, Size> {};
int main()
{
    using tuple1 = std::tuple<int, char, double>;
    static_assert(3 == std::tuple_size_v<tuple1>); // using 템플릿 사용 (C++17)
    using array3x4 = std::array<std::array<int, 3>, 4>;
    static_assert(4 == std::tuple_size<array3x4>{}); // operator std::size_t 사용
    using pair = std::pair<tuple1, array3x4>;
    static_assert(2 == std::tuple_size<pair>()); // operator() 사용
    using sub = std::ranges::subrange<char*, char*>;
    static_assert(2 == std::tuple_size<sub>::value);
    using Arr5 = Arr<int, 5>;
    static_assert(5 == std::tuple_size_v<Arr5>);
}

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2212 C++11 일부 헤더에서 cv 타입에 대한 특수화가 요구되지 않아 모호성이 발생함 요구됨

참고 항목

Structured binding (C++17) 지정된 이름들을 초기화자의 하위 객체나 튜플 요소에 바인딩합니다
튜플과 유사한 타입의 요소 타입들을 얻습니다
(클래스 템플릿)
(C++11)
임의의 개수의 튜플들을 연결하여 tuple 을 생성합니다
(함수 템플릿)