当前位置:网站首页>constexpr 函数
constexpr 函数
2022-06-29 22:37:00 【学习好烦啊】
constexpr 函数 — C++ 20
constexpr double pi = 3.14;
constexpr允许你在编译时使用典型的C++函数语法进行编程,但这并不意味之constexpr只和编译期有关
constexpr函数可以在编译期运行,也可以在运行时运行
但在以下情况constexpr函数必须在编译期运行:
constexpr函数在编译的上下文中需要,比如数组初始化,类型萃取库,断言表达式constexpr函数的值被constexpr的值需要,比如constexpr int a = func();
与模板元编程相似

- 模板元程序一定在编译期运行,但是
constexpr函数可以在编译期或运行时运行 - 模板元程序的参数可以使类型,非类型,模板
- 编译期是没有状态的,这意味着模板元程序是纯粹的函数式风格
- 在模板元程序中,不会修改值,只会在每次返回值
- 在编译期,实现一个循环所需要的可变变量是不可能实现的,所有的循环都依赖递归
- 在模板元程序中,条件执行都被模板的特化取代
对比
constexpr参数与模板元函数的参数对比

constexpr函数可以有可变的变量并修改,但是模板元函数无

- 模板元函数使用递归模拟循环

- 模板元函数使用专门化或者模板结束循环

- 模板元函数不会修改变量,而是在每次递归中返回一个新值

- 模板元函数没有返回值,直接使用值作为返回值

实例化
template <typename T>
bool isSmaller(T fir, T sec)
{
return fir < sec;
}
int main(int argc, char* argv[])
{
isSmaller(5, 10); // (1)
std::unordered_set<int> set1;
std::unordered_set<int> set2;
isSmaller(set1, set2); // (2)
}
- 检查模板语法,通常由编译器完成
- 为每个不同类型的模板参数实例化一个模板函数,第二个函数会失败,因为
std::unordered_set<int>不支持<
constexpr与此相似
什么是函数式编程思维? - 用心阁的回答 - 知乎 https://www.zhihu.com/question/28292740/answer/40336090
么是函数式编程思维? - 用心阁的回答 - 知乎 https://www.zhihu.com/question/28292740/answer/40336090
边栏推荐
- 英语没学好到底能不能做coder,别再纠结了先学起来
- 深入解析kubernetes controller-runtime
- Does rapid software delivery really need to be at the cost of security?
- Go zero micro Service Practice Series (VII. How to optimize such a high demand)
- 开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
- How ZABBIX 5.0 adds esxi6.7 to monitoring
- 啃下大骨头——排序(一)
- One click file sharing software jirafeau
- The server quickly sets up the alist integrated network disk website [pagoda panel one click deployment of alist]
- 分布式消息中间件设计
猜你喜欢

众昂矿业:萤石助力氟产业锂电建设发展

啃下大骨头——排序(一)

从零实现深度学习框架——LSTM从理论到实战【理论】

5-1系统漏洞扫描

中国数据库崛起,阿里云李飞飞:中国云数据库多种主流技术创新已领先国外

error: C2665: “QMessageBox::critical”: 4 个重载中没有一个可以转换所有参数类型

Advanced use of the optional class

math_ Basic elementary function graph (power function / exponent / logarithm / trigonometry / inverse trigonometry)
![Realizing deep learning framework from zero -- RNN from theory to practice [practice]](/img/a0/d64b69dec4a8f3a3dbc2eb47df9372.png)
Realizing deep learning framework from zero -- RNN from theory to practice [practice]

《天天数学》连载54:二月二十三日
随机推荐
Unicom warehousing | all Unicom companies that need to sell their products need to enter the general warehouse first
The client can connect to remote MySQL
Realizing deep learning framework from zero -- LSTM from theory to practice [theory]
什么是IGMP?IGMP与ICMP有啥区别?
在线文本数字识别列表求和工具
Digital tracking analysis of insurance services in the first quarter of 2022
写论文工具:LaTex在线网站
Talk about auto in MySQL in detail_ What is the function of increment
Online text digit recognition list summation tool
正如以往我们对于互联网的看法一样,我们对于互联网的认识开始变得深度
Phpspreadsheet reading and writing Excel files
服务器快速搭建AList集成网盘网站【宝塔面板一键部署AList】
111.简易聊天室14:聊天室客户端
把数组排成最小的数_数组中的逆序对(归并统计法)_数字在升序数组中出现的次数_丑数(剑指offer)
Evaluation of powerful and excellent document management software: image management, book management and document management
Realizing deep learning framework from zero -- RNN from theory to practice [practice]
Weekly Postgres world news 2022w25
MySQL lock common knowledge points & summary of interview questions
Ansible自动化运维
Unity Pac Man games, maze implementation