当前位置:网站首页>Judge whether the two types are the same
Judge whether the two types are the same
2022-07-23 09:28:00 【Hair like snow ty】
There is one in the standard library std::is_same Class template for , Used to judge whether the two types are the same , Take a look at the following test first :
int main()
{
cout << std::is_same<int, int>::value << endl;
cout << std::is_same<int, double>::value << endl;
cout << std::is_same<int, const int>::value << endl;
system("pause");
return 0;
}
result ;
How to realize one by yourself ?
template<typename T1, typename T2>
struct IsSameType
{
static const int value = 0;
};
template<typename T1>
struct IsSameType<T1,T1>
{
static const int value = 1;
};
int main()
{
cout << IsSameType<int, int>::value << endl;
cout << IsSameType<int, double>::value << endl;
cout << IsSameType<int, const int>::value << endl;
system("pause");
return 0;
}
result :
You can use a variable template to make an improvement .
Be careful : Cannot use alias template , Because the result is a value , Not a type .
template<typename T1,typename T2>
const int IsSame_v = IsSameType<T1, T2>::value;
int main()
{
cout << IsSame_v<int, int> << endl;
cout << IsSame_v<int, double><< endl;
cout << IsSame_v<int, const int><< endl;
system("pause");
return 0;
}
result :
In fact, you can also optimize class templates , as follows :
template<typename T1, typename T2>
struct IsSameType:false_type
{
};
template<typename T1>
struct IsSameType<T1,T1>:true_type
{
};
template<typename T1,typename T2>
const int IsSame_v = IsSameType<T1, T2>::value;
int main()
{
cout << IsSame_v<int, int> << endl;
cout << IsSame_v<int, double><< endl;
cout << IsSame_v<int, const int><< endl;
system("pause");
return 0;
}
result :
边栏推荐
- 2302. Count the number of subarrays with a score less than k - sliding array - double hundred code
- 一文带你了解如何用SQL处理周报数据
- Props and context in setup
- SPSS Chi-Square
- PyG利用MessagePassing搭建GCN实现节点分类
- 判断两个类型是否相同
- BCG 使用之NOTIFYICONDATA托盘
- Stream操作之 先分组再取最大值
- 2000. reverse word prefix
- Huawei applications have called the checkappupdate interface. Why is there no prompt for version update in the application
猜你喜欢

涅槃重生!字节大牛力荐大型分布式手册,凤凰架构让你浴火成神
How many points can you get on the latest UnionPay written test for test engineers?

Wallys/DR4019S/IPQ4019/11ABGN/802.11AC/high power

Salary increase artifact

35岁程序员,早到的中年危机

C语言实战之猜数游戏

Advantages of implementing automatic network performance monitoring

如何高效系统学习 MySQL?
![[C language] file operation](/img/d3/5e5ce369dd3315089b529cf69b36c0.png)
[C language] file operation

LeetCode 练习——关于二叉树的最近公共祖先两道题
随机推荐
2022.7.22-----leetcode.757
La fosse Piétinée par l'homme vous dit d'éviter les 10 erreurs courantes dans les tests automatisés
程序环境和预处理
Pytorch visualization
REPEATABLE-READ RR模式区间锁
【无标题】
VS Code快捷键设置
程序员不会 jvm?骨灰级工程师:全等着被淘汰吧!这是必会技能!
券商真的有保本型理财产品吗?
Go-Excelize API源码阅读(五)—— Close()、NewSheet()
wallys/WiFi6 MiniPCIe Module 2T2R2×2.4GHz 2x5GHz MT7915 MT7975
Double disk: what is a b+ tree? Do you know how b+ trees build ordered tables? What are the characteristics of b+ tree
php获取证书编号没有serialNumberHex只有serialNumber处理方法
1646. Recursive method of getting the maximum value in the generated array
xmpp 服务研究(一)
QT显示中文乱码
向后量子密码学迁移!美国NIST公布12家合作伙伴
C#之winform窗体的最大化、最小化、还原、关闭以及窗体的移动
PyTorch可视化
【bug 简单处理】