当前位置:网站首页>enable_ if
enable_ if
2022-06-27 13:44:00 【Hair like snow ty】
One 、enable_if The definition of
enable_if yes c++11 A class template introduced by the standard , Its use reflects c++ Compiler SFINAE characteristic . For not used enable_if For my classmates , It may seem a little abrupt , Let's start with an example .
template<typename T>
struct MEB
{
using type = T;
}
And then main Used in :
MEB<int>::type abc = 15;
It's not hard to see. MEB::type It stands for int type . After understanding the above example , So let's see enable_if Implementation of the source code , The source code is very simple , as follows :
template<bool _Test,class _Ty = void>
struct enable_if {
};
template<class _Ty>
struct enable_if<true,_Ty>
{
using type = _Ty;
}
The code above defines a enbale_if Class template , among _Test Is a non type template parameter ,_Ty Template parameters for type ; Then a enable_if A partial specialized version of , The first parameter is set to true. In fact, this partial specialization can be understood as a conditional branching statement , Such as enable_if Class template , When the first template parameter is true When , The corresponding branch is the branch of the partial specialization version , The opposite is a branch of the generalized version .
Two 、enable_if Use
stay mian Add the following code to :
std::enable_if< (3>2) >::type *myptr = nullptr;
Compile the , There are no grammatical mistakes . Because the result of this expression is true, A partial specialized version , The second template parameter is not provided in the program , The default values of the template parameters are gone ,void type , So the above code is equivalent to :
void *myptr = nullptr;
But if you change it to the following code :
std::enable_if< (3<2) >::type *myptr = nullptr;
The following prompts are provided for compilation :
This is due to the generalized version , In this version, there is no type This type alias .
ebable_if Used in function templates :
Here are some examples :
template<typename T>
typename std::enable_if<(sizeof(T) > 2) >::type funceb()
{
//...
}
Now in main Call... As follows :
funceb<int>();
No errors found after compilation , because sizeof(int)>2. That is, instantiate the function template as :
void funceb()
{
//...
}
Then if you call , It will make mistakes
funceb<char> ();
This is because sizeof(char)<2, Not meeting the conditions , No other suitable function was found , So there's an error .
c++14 Standard pair enable_if The usage of is simplified , Just add one after it _t, You can omit typename and ::type The input of , Modified as follows :
template<typename T>
typename std::enable_if_t<(sizeof(T) > 2) > funceb()
{
}
Now imagine , If you give a function template funceb() Medium enable_if_t Provide the second template parameter , That is to say
template<typename T>
std::enable_if_t<(sizeof(T) > 2), T > funceb()
{
}
So when you meet funceb(); Will be instantiated as :
int funceb()
{
//...
}
This obviously requires a return value ,
So it is revised to :
template<typename T>
std::enable_if_t<(sizeof(T) > 2), T > funceb()
{
T t = {
};
return t;
}
int main()
{
int c = funceb<int>();
cout << c << endl;
system("pause");
return 0;
}
result :
边栏推荐
- 【第27天】给定一个整数 n ,打印出1到n的全排列 | 全排列模板
- 【问题解决】Tensorflow中run究竟运行了哪些节点?
- 微服务如何拆分
- What if the win system cannot complete the update and is revoking the status change
- With the advent of the era of Internet of everything, Ruijie released a scenario based wireless zero roaming scheme
- Prometheus 2.26.0 新特性
- mysql 锁机制与四种隔离级别
- To understand again is the person in the song
- ensp云朵配置
- Today's sleep quality record 78 points
猜你喜欢

NAACL 2022 | TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络

ENSP cloud configuration

What if the win system cannot complete the update and is revoking the status change

Cesium实现卫星在轨绕行

PLM还能怎么用?

【OS命令注入】常见OS命令执行函数以及OS命令注入利用实例以及靶场实验—基于DVWA靶场

Postman如何设置成中文?(汉化)

What kind of air conditioner is this?

AXI總線

Openhgnn releases version 0.3
随机推荐
每日3题(2):检查二进制字符串字段
Prometheus 2.26.0 new features
crane:字典项与关联数据处理的新思路
【业务安全-01】业务安全概述及测试流程
Good luck today
类模板中可变参的逐步展开
scrapy
每日刷題記錄 (六)
Crane: a new way of dealing with dictionary items and associated data
MySQL locking mechanism and four isolation levels
IJCAI 2022 | 用一行代码大幅提升零样本学习方法效果,南京理工&牛津提出即插即用分类器模块
每日3题(1):找到最近的有相同 X 或 Y 坐标的点
To understand again is the person in the song
What if the win system cannot complete the update and is revoking the status change
mysql 锁机制与四种隔离级别
微服务如何拆分
A method to realize automatic renaming of pictures uploaded by WordPress
[day 27] given an integer n, print out the full permutation from 1 to n | Full Permutation template
快讯:华为启动鸿蒙开发者大赛;腾讯会议发布“万室如意”计划
打印输出数(递归方法解决)