Namespaces
Variants

std::tuple_size

ja.cppreference.net より
 
 
ユーティリティライブラリ
言語サポート
型サポート (基本型、RTTI)
ライブラリ機能テストマクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)    
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)(C++20)(C++20)    
(C++20)
Swap および 型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
共通語彙型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
ヘッダ <array>
で定義<tuple>
ヘッダ <utility>
で定義<ranges>
(C++20以上)
ヘッダ <complex>
で定義
template< class T >
struct tuple_size; // not defined
(C++26以上) (1)
template< class T >
struct tuple_size< const T >
    : std::integral_constant<std::size_t, std::tuple_size<T>::value> {};
(C++11以上) (2)
template< class T >
struct tuple_size< volatile T >
    : std::integral_constant<std::size_t, std::tuple_size<T>::value> {};
(C++11以上) (3)
(C++11以上)
template< class T >
struct tuple_size< const volatile T >
    : std::integral_constant<std::size_t, std::tuple_size<T>::value> {};
(C++20で非推奨) (4)
(C++11以上)

(C++20で非推奨)タプルライク な型の要素数へのアクセスをコンパイル時定数式として提供します。

1) プライマリテンプレートは定義されていません。型をタプルライクにするには、明示的な (完全) 特殊化または部分特殊化が必要です。
2-4) cv 修飾された型に対する特殊化は、デフォルトで対応する cv 修飾されていないバージョンの value を再利用します。

std::tuple_size はコア言語と相互作用します: タプルライクな場合に 構造化束縛 のサポートを提供できます。

(2-4) は SFINAE フレンドリです: std::tuple_size<T>::value を未評価オペランドとして扱ったときに不適格となる場合、メンバ value は提供されません。アクセスチェックは tuple_size および T とは無関係な文脈で行われたかのように実行されます。式の直接の文脈の妥当性のみが考慮されます。これにより

#include <utility>

struct X { int a, b; };
const auto [x, y] = X(); // structured binding declaration first attempts
                         // tuple_size<const X> which attempts to use tuple_size<X>::value,
                         // then soft error encountered, binds to public data members
(C++17以上)

特殊化

標準ライブラリは、標準ライブラリ型に対して以下の特殊化を提供します:

tupleのサイズを取得する


(クラステンプレートの特殊化)

pairのサイズを取得する
(クラステンプレートの特殊化)
arrayのサイズを取得する
(クラステンプレートの特殊化)
std::ranges::subrange のサイズを取得する
(クラステンプレートの特殊化)
std::complex のサイズを取得する
(クラステンプレートの特殊化)

std::tuple_sizeのすべての特殊化は、あるNに対して UnaryTypeTrait を満たし、 基本特性 は std:: integral_constant < std:: size_t , N > となります。

ユーザーはプログラム定義型に対して std::tuple_size を特殊化して、それらをタプル様にすることができます。プログラム定義の特殊化は上記の要件を満たさなければなりません。

通常、cv修飾されていない型に対する特殊化のみをカスタマイズする必要があります。

ヘルパー変数テンプレート

ヘッダーで定義 <tuple>
template < class T >
constexpr std:: size_t tuple_size_v = tuple_size < T > :: value ;
(C++17以降)

std:: integral_constant から継承

メンバ定数

value
[static]
標準特殊化の場合、タプル様型 T の要素数
(公開静的メンバ定数)

メンバ関数

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 <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 を作成する
(関数テンプレート)