当前位置:网站首页>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 .
边栏推荐
- Compilation principle - top-down analysis and recursive descent analysis construction (notes)
- 开源与安全的“冰与火之歌”
- OpenEuler 会长久吗
- MySQL stored procedure
- Unity粒子特效系列-闪星星的宝箱
- The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
- STM32 key state machine 2 - state simplification and long press function addition
- Pytest learning ----- detailed explanation of the request for interface automation test
- 关于这次通信故障,我想多说几句…
- 微信为什么使用 SQLite 保存聊天记录?
猜你喜欢
C语言通过指针交换两个数
Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"
2022年大厂Android面试题汇总(二)(含答案)
2019阿里集群数据集使用总结
Interview shock 62: what are the precautions for group by?
FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
There is a gap in traditional home decoration. VR panoramic home decoration allows you to experience the completion effect of your new house
Unity小技巧 - 绘制瞄准准心
關於這次通信故障,我想多說幾句…
随机推荐
面试突击62:group by 有哪些注意事项?
【.NET CORE】 请求长度过长报错解决方案
Codeforces Round #803 (Div. 2)
重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
2022年大厂Android面试题汇总(二)(含答案)
Interview assault 63: how to remove duplication in MySQL?
Basic configuration and use of spark
面试突击62:group by 有哪些注意事项?
[introduction to MySQL] the first sentence · first time in the "database" Mainland
Interview shock 62: what are the precautions for group by?
Shell input a string of numbers to determine whether it is a mobile phone number
關於這次通信故障,我想多說幾句…
中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
declval(指导函数返回值范例)
队列的实现
李書福為何要親自掛帥造手機?
The latest financial report release + tmall 618 double top, Nike energy leads the next 50 years
Unity粒子特效系列-闪星星的宝箱
VR全景婚礼,帮助新人记录浪漫且美好的场景
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升