std::type_identity
From cppreference.com
| ヘッダ <type_traits> で定義 |
||
| template< class T > struct type_identity; |
(C++20以降) | |
T という名前のメンバー typedef type を提供します (つまり、恒等変換)。
プログラムが std::type_identity の特殊化を追加した場合、その動作は未定義です。
目次 |
[編集] メンバー型
| 名前 | 定義 |
type
|
T
|
[編集] ヘルパー型
| template< class T > using type_identity_t = type_identity<T>::type; |
(C++20以降) | |
[編集] 可能な実装
template<class T> struct type_identity { using type = T; }; |
[編集] 注記
std::type_identity は、テンプレート引数推論における非推論コンテキストを確立するために使用できます。
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_type_identity |
201806L |
(C++20) | std::type_identity
|
[編集] 例
このコードを実行
#include <iostream> #include <type_traits> template<class T> T foo(T a, T b) { return a + b; } template<class T> T bar(T a, std::type_identity_t<T> b) { return a + b; } int main() { // foo(4.2, 1); // error, deduced conflicting types for 'T' std::cout << bar(4.2, 1) << '\n'; // OK, calls bar<double> }
出力
5.2
[編集] 関連項目
| (C++20) |
引数を変更せずにそのまま返す関数オブジェクト (クラス) |