std::tuple_size
| ヘッダ <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で非推奨)タプルライク な型の要素数へのアクセスをコンパイル時定数式として提供します。
value を再利用します。|
(2-4) は SFINAE フレンドリです: #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以上) |
特殊化
標準ライブラリは、標準ライブラリ型に対して以下の特殊化を提供します:
|
(C++11)
|
tupleのサイズを取得する
|
|
(C++11)
|
pairのサイズを取得する
(クラステンプレートの特殊化) |
|
(C++11)
|
arrayのサイズを取得する
(クラステンプレートの特殊化) |
|
std::ranges::subrange
のサイズを取得する
(クラステンプレートの特殊化) |
|
|
(C++26)
|
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)
|
タプルライクな型の要素型を取得する
(クラステンプレート) |
|
(C++11)
|
任意の数のタプルを連結して
tuple
を作成する
(関数テンプレート) |