当前位置:网站首页>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 .
边栏推荐
- Establishment of graphical monitoring grafana
- [Android] kotlin code writing standardization document
- Zen integration nails, bugs, needs, etc. are reminded by nails
- EasyCVR电子地图中设备播放器loading样式的居中对齐优化
- kivy教程之在 Kivy 中支持中文以构建跨平台应用程序(教程含源码)
- It doesn't make sense without a distributed gateway
- 78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
- Alertmanager sends the alarm email and specifies it as the Alibaba mailbox of the company
- 李書福為何要親自掛帥造手機?
- Alibaba brand data bank: introduction to the most complete data bank
猜你喜欢

中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障

历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱

Summary of Android interview questions of Dachang in 2022 (II) (including answers)

Four processes of program operation

Open source and safe "song of ice and fire"

關於這次通信故障,我想多說幾句…

Spark accumulator and broadcast variables and beginners of sparksql

Establishment of graphical monitoring grafana
![Jerry's updated equipment resource document [chapter]](/img/6c/17bd69b34c7b1bae32604977f6bc48.jpg)
Jerry's updated equipment resource document [chapter]

Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
随机推荐
趣-关于undefined的问题
MarkDown语法——更好地写博客
[introduction to MySQL] the first sentence · first time in the "database" Mainland
EasyCVR授权到期页面无法登录,该如何解决?
In terms of byte measurement with an annual salary of 30W, automated testing can be learned in this way
最新财报发布+天猫618双榜第一,耐克蓄力领跑下个50年
队列的实现
How to use scroll bars to dynamically adjust parameters in opencv
Shell input a string of numbers to determine whether it is a mobile phone number
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
High precision operation
Solution qui ne peut pas être retournée après la mise à jour du navigateur Web flutter
RepPoints:可形变卷积的进阶
Growth of operation and maintenance Xiaobai - week 7
Spark accumulator and broadcast variables and beginners of sparksql
Jerry's watch deletes the existing dial file [chapter]
The latest financial report release + tmall 618 double top, Nike energy leads the next 50 years
Nodejs developer roadmap 2022 zero foundation Learning Guide
Jerry's watch reading setting status [chapter]
Zen integration nails, bugs, needs, etc. are reminded by nails