栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

constexpr和consteval --- C++ 20

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

constexpr和consteval --- C++ 20

constexpr和consteval — C++ 20 标准库容器和算法库对constexpr 的应用

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

Algorithms library - cppreference.com

如下:

#include 
#include 
#include 
#include 
#include 
#include 

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 lambda


	std::cout << maxValue1 << std::endl;


	constexpr int maxValue = maxElement();
	std::cout << std::format("maxElement: {}", maxValue);
}

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

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

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

#include 
#include 
#include 
#include 
#include 
#include 
#include 


#include 

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 
#include 
#include 
#include 
#include 
#include 
#include 


#include 

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的所有要求

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/976854.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号