constexpr和consteval --- C++ 20

constexprconsteval — C++ 20

标准库容器和算法库对constexpr 的应用

C++20 中大量的算法和容器可以使用constexpr,这意味着你甚至可以再编译期vector<int>进行排序

Algorithms library - cppreference.com

如下:

#include <iostream>
#include <ranges>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <format>constexpr int maxElement()
{std::vector myVec{1, 4, 5, 7, 23, 4};std::sort(myVec.begin(), myVec.end());return myVec.back();
}int main(int argc, char* argv[])
{constexpr int maxValue1 = []()-> int{std::vector myVec = {1, 2, 4, 3}; std::sort(myVec.begin(), myVec.end());return myVec.back();}(); //  immediately-invoked lambdastd::cout << maxValue1 << std::endl;constexpr int maxValue = maxElement();std::cout << std::format("maxElement: {}", maxValue);
}

image-20220601215824311

  • immediately-invoked lambda : 即调用函数表达式:先创建Lambda表达式,并不分配给任何闭包对象,然后它被( )调用

Transient Allocation (瞬时分配内存)

Transient Allocation: 编译期申请的内存也会在编译期释放

C++不支持 non-transient constexpr allocation:编译期申请的内存提升为静态在运行时继续使用

#include <iostream>
#include <ranges>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <format>
#include <memory>#include <memory>constexpr auto correctRelease() {auto* p = new int[2020];delete[] p;return 2020;
}constexpr auto forgottenRelease() { // (1)auto* p = new int[2020];return 2020;
}constexpr auto falseRelease() {     // (3)auto* p = new int[2020];delete p;                       // (2)return 2020;
}int main() {constexpr int res1 = correctRelease();// constexpr int res2 = forgottenRelease();// constexpr int res3 = falseRelease();}

注释掉的函数编译失败,因为内存没有成对的申请和释放

constexpr有个缺点:无法确定是在编译期还是运行时执行

#include <iostream>
#include <ranges>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <format>
#include <memory>#include <memory>constexpr int constexprFunction(int arg)
{return arg * arg;
}int main()
{static_assert(constexprFunction(10) == 100); // (1)int arrayNewWithConstExpressiomFunction[constexprFunction(100)]; // (2)constexpr int prod = constexprFunction(100); // (3)int a = 100;int runTime = constexprFunction(a); // (4)int runTimeOrCompiletime = constexprFunction(100); // (5) 编译期和运行时都可以执行
}

所以C++20 就有了 consteval,一定在编译期执行

consteval

只能在编译期执行

consteval int sqr(int n) {return n * n;
}
  • 每次调用即时函数都会创建一个编译期常量

  • 不能应用于析构函数,或者申请或释放内存的函数

  • 满足constexpr的所有要求

consteval int sqr(int n) {return n * n;
}
  • 每次调用即时函数都会创建一个编译期常量

  • 不能应用于析构函数,或者申请或释放内存的函数

  • 满足constexpr的所有要求

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/407964.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

西藏攻略集锦

西藏旅游局针对初此进藏旅游的驴友提供实用资讯本文为西藏旅游局针对初次进藏的驴友提供实用资讯&#xff0c;供各位参考。 1、什么是高原反应&#xff1f;高原反应有哪些症状&#xff1f; 高原反应是人到达一定海拔高度后&#xff0c;身体为适应因海拔高度…

将select中的项从一个移动到另一个select中

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns"http://www.w3.org/1999/xhtml" ><head> <title>将select中的项互相移动…

函数模板(参考《C++ Templates 英文版第二版》)

函数编程(参考《C Templates 英文版第二版》) Chapter 1 函数模板 1.1 一窥函数模板 template<class T> T max(T a, T b) {return b < a ? a : b; }#include "max1.hpp" #include <iostream> #include <string> #include <format>int…

Android FTP Server 1

下面介绍几种Android 版本的FTP Server : virtualdataline Virtual Data Line is a software that you can manage files of the phone on you pc without data line . Phone files can be copied to a computer and computer files can copied to the phone. With this …

Switcher ---Vista Areo 工具

推荐一个Vista Areo主题工具Switcher.可以从这里 [url]http://insentient.net/[/url] 获取 自由、创新、研究、探索…… 转载于:https://blog.51cto.com/shanyou/74271

很好的测试智商看看你能回答出来多少?一共75道!!

【1】假设有一个池塘&#xff0c;里面有无穷多的水。现有2个空水壶&#xff0c;容积分别为5升和6升。问题是如何只用这2个水壶从池塘里取得3升的水。 【2】周雯的妈妈是豫林水泥厂的化验员。 一天&#xff0c;周雯来到化验室做作业。做完后想出去玩。 "等等&#xff0c;妈…

react(84)--多张图片

<Form.Item label"上传封面图片"><BaseUploadImageonRef{(ref) > {this.upload ref;}}value{coverPaths}multiple/></Form.Item>

【ES实战】Elasticsearch6开始的CCR

【ES实战】学习使用Elasticsearch6开始的CCR 本文涉及官网文章地址 OverviewRequirements for leader indicesAutomatically following indicesGetting started with cross-cluster replicationUpgrading clusters CCR > Cross-cluster replication 文章目录 【ES实战】学…

可变参数模板(参考《C++ Templates 英文版第二版》)

可变参数模板(参考《C Templates 英文版第二版》) Chapter 4 可变参数模板 自从C11,模板可以接受可变数量的参数 4.1 可变参数模板 可以定义模板,去接受无限数量的模板参数 这种行为的模板叫做可变参数模板 4.1.1 例子 #include <iostream>template<typename T…

Curiously Recurring Template Pattern奇怪的模板递归 --- C++20

Curiously Recurring Template Pattern 奇怪的模板递归 — C20 我们都知道C有静态多态和动态多态,动态多态通过虚函数表实现,他的缺点就是对效率产生一点点影响 可以用CRTP解决这个问题 我们先举一个动态多态的例子: #include <iostream> using namespace std;class …

关于table的中元素对齐方式的注意点

情形一&#xff1a;<td></td>中嵌套了div或table元素。 1。td中的内部元素(如div)设置了height属性&#xff0c;td中设置了vertical-align的情况下&#xff0c;在IE中&#xff0c;td的vertical-align不起作用&#xff0c;但在firefox中是起作用的。 2。如果td中未…

不要带我去看鱼了

我在企业里面上班。每天中午都得回家吃午饭。儿子才3岁。每每见我总是要我抱&#xff0c;我经常出差什么的&#xff0c;儿子都怕我走了。每次我要离开的时候&#xff0c;只好让老婆抱着儿子遮住&#xff0c;让我悄悄地离开。儿子便猛哭。老婆只好带着儿子到家门口的池塘旁边去看…

char 与 String 相等比较

这是一个相当2 相当基础 相当没有意义的帖子&#xff1b;但今天因为这个问题引发了一个bug。小细节也很重要&#xff01;&#xff01;&#xff01; char a1;// char b2dsf; //char 表示单个字符 char c1; //不需要单引号也能表示// System.out.println(a.equals(&q…

我眼中的金蝶ERP

在我的博克回复里&#xff0c;有个小雨的博友问我是否了解金蝶ERP&#xff0c;我答应给她说说金蝶ERP&#xff0c;所以就有了这份文章。在我们国内&#xff0c;最出名的或者说市场占有率前两位的就是用友和金蝶了。至于到底谁排名先排名后一点&#xff0c;这不是我想说的范围。…

什么是JDO

From: http://blog.csdn.net/wp_84/archive/2007/09/02/1769481.aspx Java数据对象(Java Data Objects,JDO)是一个应用程序接口(API),它是Java程序员能够间接地访问数据库,也就是说,不需使用直接的结构化查询语言(SQL)语句.JDO是作为Java数据库连接(JDBC)的一个补充来介绍的,而…

PROJECT #0 - C++ PRIMER [CMU 15-445645]笔记

PROJECT #0 - C PRIMER [CMU 15-445/645]笔记 这是数据库领域的一门课程, 由卡内基梅隆大学副教授Andy Pavlo授课, 目前在网上有授课视频资料、实验以及配套的在线测评环境 (限时开放至2021年12月31日) 环境: wsl2 Clion Project #0 - C Primer 还是很简单的,主要目的是让…

简单JS实现对表的行的增删

这段代码非常的简单&#xff0c;仅仅作为自己的一个小小的记录&#xff01; ok&#xff0c;先上一个简单的图例&#xff0c;效果如下&#xff08;注意&#xff1a;这只是一个简单的例子&#xff0c;不过可以根据这个简单的例子&#xff0c;变化出更为复杂的效果&#xff09;&am…