Namespaces
Variants

std:: tuple_size <std::tuple>

From cppreference.net
Utilities library
ヘッダーで定義 <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)
(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 <iostream>
#include <tuple>
template <class T>
void test(T value)
{
    int a[std::tuple_size_v<T>]; // コンパイル時に使用可能
    std::cout << std::tuple_size<T>{} << ' ' // または実行時に
              << sizeof a << ' '
              << sizeof value << '\n';
}
int main()
{
    test(std::make_tuple(1, 2, 3.14));
}

出力例:

3 12 16

関連項目

Structured binding (C++17) 指定された名前を初期化子の部分オブジェクトまたはタプルの要素にバインドする
(C++11)
タプルライクな型の要素数を取得する
(クラステンプレート)
pair のサイズを取得する
(クラステンプレートの特殊化)
array のサイズを取得する
(クラステンプレートの特殊化)
std::ranges::subrange のサイズを取得する
(クラステンプレートの特殊化)
タプルの指定された要素にアクセスする
(関数テンプレート)