当前位置:网站首页>std::true_ Type and std:: false_ type
std::true_ Type and std:: false_ type
2022-07-06 18:03:00 【Hair like snow ty】
One 、 know std::true_type and std::false_type
std::true_type and std::false_type It's actually a type alias , Source code is as follows :
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>;
We can know through the source code , In fact, it is a structure template
Let's take a look at its simple use :
int main()
{
std::true_type a;
std::false_type b;
cout <<a << endl;
cout <<b<< endl;
system("pause");
return 0;
}
result :
Two 、 And true and false The difference between
1.true_type、false_type It stands for type ( Class types ), and true、false Represents the value , Don't confuse .
2. There are occasions where types are used , Value has value and can be used in occasions , For example, function return type , use true_type Means to return a true Type value , use false_type Means to return a false Type value . Some students , May be confused , Because with bool Type can achieve the same effect . This is actually a wrong understanding , because bool Can represent true Can also represent false, and true_type Type represents true,false_type Type represents false.
3、 ... and 、 Why use std::true_type and std::false_type
First remember the following points
(1)TrueType and FalseType Represents a class type ,TrueType(std::true_type) Represents a kind of true( really ) The meaning of , and False_Type(std::false_type) Represents a kind of false( false ) The meaning of .
(2) It is generally inherited as a base class . When inherited as a base class , Derived classes also have the meaning of true or false .
(3) It can be used as a return type , such as :
FalseType myfunc1(); // return “ false ” This meaning
TrueType myfunc2(); // return “ really ” This meaning
There are the following examples :
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;
}
result :
The above code , When the second parameter is true when ,T by int,false when ,T by string type , Reasonably speaking, it should be able to compile , Then why can't I compile here ? In fact, this is some consideration of the compiler , The compiler can judge the execution when compiling AClass Which branch does the class template constructor take , But from the perspective of compiling code , No matter when if Conditional branch , still else Conditional branch , Compilers will compile , So the compiler compiles to T a = “abc”; The code will report an error . You can use compile time if sentence (if constexpr) To solve , as follows :
template<typename T,bool vaxl>
struct AClass
{
AClass()
{
if constexpr(vaxl)
{
T a = 15;
}
else
{
T a = "abc";
}
}
};
result :
Next use true_type,false_type solve
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";
}
};
This example is aimed at std::true_type,std::false_type Simple application of type , Extraction technology will be explained later .
边栏推荐
- Spark accumulator and broadcast variables and beginners of sparksql
- How to solve the error "press any to exit" when deploying multiple easycvr on one server?
- RepPoints:可形变卷积的进阶
- 基本磁盘与动态磁盘 RAID磁盘冗余阵列区分
- VR全景婚礼,帮助新人记录浪漫且美好的场景
- 在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?
- 编译原理——自上而下分析与递归下降分析构造(笔记)
- Shell input a string of numbers to determine whether it is a mobile phone number
- Jerry's watch reads the file through the file name [chapter]
- 78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
猜你喜欢

传统家装有落差,VR全景家装让你体验新房落成效果

Open source and safe "song of ice and fire"

FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog

Interview assault 63: how to remove duplication in MySQL?

Easy introduction to SQL (1): addition, deletion, modification and simple query

Distinguish between basic disk and dynamic disk RAID disk redundant array

OpenCV中如何使用滚动条动态调整参数

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

开源与安全的“冰与火之歌”

Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
随机推荐
After entering Alibaba for the interview and returning with a salary of 35K, I summarized an interview question of Alibaba test engineer
node の SQLite
2022年大厂Android面试题汇总(一)(含答案)
李书福为何要亲自挂帅造手机?
Establishment of graphical monitoring grafana
微信小程序中给event对象传递数据
Jerry's watch reads the file through the file name [chapter]
Pytorch extract middle layer features?
基于STM32+华为云IOT设计的智能路灯
Distinguish between basic disk and dynamic disk RAID disk redundant array
Why should Li Shufu personally take charge of building mobile phones?
Reppoints: advanced order of deformable convolution
分布式不来点网关都说不过去
队列的实现
TCP packet sticking problem
1700C - Helping the Nature
编译原理——预测表C语言实现
The shell generates JSON arrays and inserts them into the database
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
2019阿里集群数据集使用总结