std::type_identity
來自 cppreference.com
定義於標頭檔案 <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) |
返回其引數不變的函式物件 (類) |