std::tuple_size<std::pair>
From cppreference.com
| ヘッダ <utility> で定義 |
||
| template< class T1, class T2 > struct tuple_size<std::pair<T1, T2>> |
(C++11以降) | |
ペアに対する std::tuple_size の部分特殊化は、ペアの要素数をコンパイル時に取得するための方法を提供します。これはタプルライクな構文を使用し、常に 2 です。
目次 |
std::integral_constant から継承
メンバ定数
| value [static] |
定数値 2 (公開静的メンバ定数) |
メンバ関数
| 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> #include <utility> template<class T> void test([[maybe_unused]]T t) { [[maybe_unused]] int a[std::tuple_size<T>::value]; // can be used at compile time std::cout << std::tuple_size<T>::value << '\n'; // or at run time } int main() { test(std::make_tuple(1, 2, 3.14)); test(std::make_pair(1, 3.14)); }
出力
3 2
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 2313 | C++11 | pair の特殊化は integral_constant から派生する必要がなかった |
必要 |
[編集] 関連項目
| 構造化束縛 (C++17) | 指定された名前を初期化子のサブオブジェクトまたはタプル要素に束縛します |
| (C++11) |
タプルライクな型の要素数を取得する (クラステンプレート) |
| (C++11) |
array のサイズを取得する(クラステンプレート特殊化) |
| (C++11) |
のサイズを取得する
|
| std::ranges::subrangeのサイズを取得する (クラステンプレート特殊化) | |
pairの要素の型を取得する(クラステンプレート特殊化) |