当前位置:网站首页>std::true_type和std::false_type
std::true_type和std::false_type
2022-07-06 10:05:00 【发如雪-ty】
一、认识std::true_type和std::false_type
std::true_type和std::false_type实际上是类型别名,源码如下:
template <class _Ty,
_Ty _Val>
struct integral_constant {
// convenient template for integral constant types
static constexpr _Ty value = _Val;
using value_type = _Ty;
using type = integral_constant;
constexpr operator value_type() const noexcept {
// return stored value
return value;
}
_NODISCARD constexpr value_type operator()() const noexcept {
// return stored value
return value;
}
};
// ALIAS TEMPLATE bool_constant
template <bool _Val>
using bool_constant = integral_constant<bool, _Val>;
using true_type = bool_constant<true>;
using false_type = bool_constant<false>;
通过源码我们可以知道,其实它就是一个结构体模板
下面看看它的简单使用:
int main()
{
std::true_type a;
std::false_type b;
cout <<a << endl;
cout <<b<< endl;
system("pause");
return 0;
}
结果:
二、与true和false的区别
1.true_type、false_type代表的是类型(类类型),而true、false代表的是值,不要弄混。
2.类型有类型用到的场合,值有值能用到的场合,比如函数返回类型,用true_type表示返回一个true类型的值,用false_type表示返回一个false类型的值。有的同学,可能会疑惑,因为用bool类型也能达到同样的效果。这其实是错误的认识,因为bool既能代表true也能代表false,而true_type类型代表的就是true,false_type类型代表的就是false.
三、为什么要使用std::true_type和std::false_type
先记住下面几点
(1)TrueType和FalseType代表一个类类型,TrueType(std::true_type)代表一种true(真)的含义,而False_Type(std::false_type)代表一种false(假)的含义。
(2)一般是当作基类被继承。当作为基类被继承时,派生类也就具备了真或假的这种意味。
(3)可以当作一种返回类型被使用,比如:
FalseType myfunc1(); //返回“假”这种含义
TrueType myfunc2(); //返回“真”这种含义
有如下的例子:
template<typename T,bool vaxl>
struct AClass
{
AClass()
{
if (vaxl)
{
T a = 15;
}
else
{
T a = "abc";
}
}
};
int main()
{
AClass<int, true> obj1;
AClass<string, false> obj2;
system("pause");
return 0;
}
结果:
上面的代码,当第二个参数为true时,T为int,false时,T为string类型,按道理说应该能编译过啊,那这里为什么编译不过呢?其实这是编译器的一些考量,编译器能够在编译的时候判断出执行AClass类模板构造函数走哪个分支,但是从编译出代码的角度来讲,不管时if条件分支,还是else条件分支,编译器都会去编译,所以编译器编译到T a = “abc”;代码的时会报错。这里可以使用编译期间if语句(if constexpr)来解决,如下:
template<typename T,bool vaxl>
struct AClass
{
AClass()
{
if constexpr(vaxl)
{
T a = 15;
}
else
{
T a = "abc";
}
}
};
结果:
接下来使用true_type,false_type解决
template<typename T,bool vaxl>
struct AClass
{
AClass()
{
fun(bool_constant<vaxl>());
}
void fun(std::true_type)
{
T a = 15;
}
void fun(std::false_type)
{
T a = "abc";
}
};
这个例子时一个针对std::true_type,std::false_type类型的简单应用,后面再萃取技术会继续讲解。
边栏推荐
- The art of Engineering (3): do not rely on each other between functions of code robustness
- Binary search strategy
- Unity粒子特效系列-闪星星的宝箱
- Distributed (consistency protocol) leader election (dotnext.net.cluster implements raft election)
- Easy introduction to SQL (1): addition, deletion, modification and simple query
- IP, subnet mask, gateway, default gateway
- 趣-关于undefined的问题
- 微信小程序获取手机号
- 开源与安全的“冰与火之歌”
- [introduction to MySQL] the first sentence · first time in the "database" Mainland
猜你喜欢
2022年大厂Android面试题汇总(一)(含答案)
Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
SAP UI5 框架的 manifest.json
There is a gap in traditional home decoration. VR panoramic home decoration allows you to experience the completion effect of your new house
视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
传统家装有落差,VR全景家装让你体验新房落成效果
基于STM32+华为云IOT设计的智能路灯
Optimization of middle alignment of loading style of device player in easycvr electronic map
78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
随机推荐
[getting started with MySQL] fourth, explore operators in MySQL with Kiko
The easycvr authorization expiration page cannot be logged in. How to solve it?
Awk command exercise
Distinguish between basic disk and dynamic disk RAID disk redundant array
C语言通过指针交换两个数
历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱
C语言指针*p++、*(p++)、*++p、*(++p)、(*p)++、++(*p)对比实例
Optimization of middle alignment of loading style of device player in easycvr electronic map
Unity粒子特效系列-闪星星的宝箱
kivy教程之在 Kivy 中支持中文以构建跨平台应用程序(教程含源码)
偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
Single responsibility principle
OliveTin能在网页上安全运行shell命令(上)
在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?
【Android】Kotlin代码编写规范化文档
Nodejs 开发者路线图 2022 零基础学习指南
10 advanced concepts that must be understood in learning SQL
EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
1700C - Helping the Nature
Unity tips - draw aiming Center