Namespaces
Variants

std:: type_identity

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

type_identity
(C++20)
(C++11)
(C++17)
Compile-time rational arithmetic
Compile-time integer sequences
ヘッダーで定義 <type_traits>
template < class T >
struct type_identity ;
(C++20以降)

T を指すメンバー型 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); // エラー: 'T' に対して矛盾する型が推論されました
    std::cout << bar(4.2, 1) << '\n';  // OK: bar<double> を呼び出します
}

出力:

5.2

関連項目

(C++20)
引数を変更せずに返す関数オブジェクト
(クラス)