名稱空間
變體
操作

std::type_identity

來自 cppreference.com
< cpp‎ | 型別
 
 
超程式設計庫
型別特性
型別類別
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
型別屬性
(C++11)
(C++11)
(C++14)
(C++11)(C++26 中已棄用)
(C++11)(直到 C++20*)
(C++11)(C++20 中已棄用)
(C++11)
型別特性常量
元函式
(C++17)
支援的操作
關係與屬性查詢
型別修改
(C++11)(C++11)(C++11)
型別轉換
(C++11)(C++23 中已棄用)
(C++11)(C++23 中已棄用)
(C++11)
(C++11)(直到 C++20*)(C++17)

type_identity
(C++20)
(C++11)
(C++17)
編譯時有理數算術
編譯時整數序列
 
定義於標頭檔案 <type_traits>
template< class T >
struct type_identity;
(C++20 起)

提供成員 typedef type,其指代 T(即恆等變換)。

如果程式為 std::type_identity 新增特化,則行為未定義。

目錄

[編輯] 成員型別

名稱 定義
型別 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)
返回其引數不變的函式物件
(類) [編輯]