std::type_identity
来自 cppreference.cn
定义于头文件 <type_traits> |
||
template< class T > struct type_identity; |
(自 C++20 起) | |
提供成员 typedef type
,其命名为 T
(即,identity 转换)。
如果程序为 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
可用于在模板实参推导中建立 非推导语境 。
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__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) |
返回其参数不变的函数对象 (类) |