当前位置:网站首页>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类型的简单应用,后面再萃取技术会继续讲解。
边栏推荐
- Unity particle special effects series - treasure chest of shining stars
- C # nanoframework lighting and key esp32
- It doesn't make sense without a distributed gateway
- Selected technical experts from China Mobile, ant, SF, and Xingsheng will show you the guarantee of architecture stability
- 遠程代碼執行滲透測試——B模塊測試
- Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
- MySQL stored procedure
- Essai de pénétration du Code à distance - essai du module b
- BearPi-HM_ Nano development board "flower protector" case
- Unity tips - draw aiming Center
猜你喜欢

Unity小技巧 - 绘制瞄准准心

Pytest learning ----- pytest confitest of interface automation test Py file details

sql语句优化,order by desc速度优化

Manifest of SAP ui5 framework json

The solution that flutterweb browser cannot be rolled back after refreshing

Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)

Spark accumulator and broadcast variables and beginners of sparksql

Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day

Pytorch extract middle layer features?

EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
随机推荐
重磅!蚂蚁开源可信隐私计算框架“隐语”,主流技术灵活组装、开发者友好分层设计...
Pytest learning ----- pytest confitest of interface automation test Py file details
Video fusion cloud platform easycvr adds multi-level grouping, which can flexibly manage access devices
JMeter interface test response data garbled
VR panoramic wedding helps couples record romantic and beautiful scenes
[rapid environment construction] openharmony 10 minute tutorial (cub pie)
The art of Engineering
Solution qui ne peut pas être retournée après la mise à jour du navigateur Web flutter
编译原理——自上而下分析与递归下降分析构造(笔记)
Shell input a string of numbers to determine whether it is a mobile phone number
Essai de pénétration du Code à distance - essai du module b
Summary of Android interview questions of Dachang in 2022 (I) (including answers)
F200——搭载基于模型设计的国产开源飞控系统无人机
容器里用systemctl运行服务报错:Failed to get D-Bus connection: Operation not permitted(解决方法)
Unity小技巧 - 绘制瞄准准心
What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
ASEMI整流桥DB207的导通时间与参数选择
二分(整数二分、实数二分)
Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
关于这次通信故障,我想多说几句…