(C++20) consteval立即函数

由来

在C++11中推出了constexpr使得对象或者函数能够具有常量性质并能在编译器确定。但是对于constexpr修饰的函数来说,无法保证严格的在编译器确定。

下面这段代码,fun1正常编译运行,但是到了fun2就会编译失败。

constexpr int square(int x) {
   
    return x * x;
}

void fun1(int x) {
   
    square(10);
    square(x);
}

void fun2(int x) {
   
    constexpr int num1 = square(10);
    // error: 'x' is not a constant expression
    // constexpr int num2 = square(x);
}

int main(int argc, char** argv) {
   
}

consteval立即函数

consteval 说明符 (C++20 起) - cppreference.com

为了能够强制在编译期间计算,C++20推出了consteval关键字用于修饰函数。强制要求该函数必须表现为常量性质

consteval int square(int x) {
   
    return x * x;
}

上下文的常量性质

编译器会自动检测上下文的常量性质,因此下面这段代码也是可性的。

constexpr int square(int x) {
   
    return x * x;
}

// constexpr函数 调用 constexpr函数
constexpr int re_square(int x) {
   
    return square(square(x));
}

int main() {
   
    // 上下文常量性质
    const int     x    = 10;
    constexpr int num1 = square(x);
    constexpr int num2 = re_square(x);
}

lambda表达式

说到函数怎么能少得了lambda函数,在参数后添加关键字即可。

int main() {
   
    auto square = [](int x) constexpr {
    return x * x; };

    const int     x   = 10;
    constexpr int num = square(x);
}

编译期间确定

无法获取函数指针

新手可能不太了解什么叫编译期间确定。

这里举个例子,一般函数我们可以获取它的地址,并赋到一个函数指针上。

但是consteval函数不行。因为它根本不会产生函数实例!

int fun1(int x) {
   
    return x + 10;
}

consteval int fun2(int x) {
   
    return x + 10;
}

int main() {
   
    int (*funPtr1)(int) = &fun1;
    // error: taking address of an immediate function ‘consteval int fun2(int)’
    // int (*funPtr2)(int) = &fun2;
}

查看汇编

再具体的我们来查看汇编代码,便会一目了然!

现在有如下的代码:

int funnnnnn000000000000000000000000(int x) {
   
    return x + 10;
}

constexpr int funnnnnn111111111111111111111111(int x) {
   
    return x + 10;
}

consteval int funnnnnn222222222222222222222222(int x) {
   
    return x + 10;
}

int main() {
   
    const int x = 10;
    funnnnnn000000000000000000000000(x);
    funnnnnn111111111111111111111111(x);
    funnnnnn222222222222222222222222(x);
}

环境与指令:

gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04) 
g++ -std=c++20 test.cpp -S

生成的汇编代码:

	.file	"test.cpp"
	.text
	.globl	_Z32funnnnnn000000000000000000000000i
	.type	_Z32funnnnnn000000000000000000000000i, @function
_Z32funnnnnn000000000000000000000000i:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	%edi, -4(%rbp)
	movl	-4(%rbp), %eax
	addl	$10, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	_Z32funnnnnn000000000000000000000000i, .-_Z32funnnnnn000000000000000000000000i
	.section	.text._Z32funnnnnn111111111111111111111111i,"axG",@progbits,_Z32funnnnnn111111111111111111111111i,comdat
	.weak	_Z32funnnnnn111111111111111111111111i
	.type	_Z32funnnnnn111111111111111111111111i, @function
_Z32funnnnnn111111111111111111111111i:
.LFB1:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	%edi, -4(%rbp)
	movl	-4(%rbp), %eax
	addl	$10, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE1:
	.size	_Z32funnnnnn111111111111111111111111i, .-_Z32funnnnnn111111111111111111111111i
	.text
	.globl	main
	.type	main, @function
main:
.LFB3:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$16, %rsp
	movl	$10, -4(%rbp)
	movl	$10, %edi
	call	_Z32funnnnnn000000000000000000000000i
	movl	$10, %edi
	call	_Z32funnnnnn111111111111111111111111i
	movl	$0, %eax
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE3:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

可见普通函数和constexpr函数都可以在汇编函数中展现,但是consteval函数并没有,这也正解释了为什么为什么不能获取函数指针。




END

相关推荐

  1. (C++20) consteval立即函数

    2023-12-06 11:20:05       65 阅读
  2. c++20 constexpr consteval

    2023-12-06 11:20:05       31 阅读
  3. C++(20):普通函数的参数使用auto声明

    2023-12-06 11:20:05       61 阅读
  4. <span style='color:red;'>c</span>++<span style='color:red;'>20</span>

    c++20

    2023-12-06 11:20:05      45 阅读
  5. C语言从头学21——函数

    2023-12-06 11:20:05       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-06 11:20:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-06 11:20:05       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-06 11:20:05       82 阅读
  4. Python语言-面向对象

    2023-12-06 11:20:05       91 阅读

热门阅读

  1. map 和 flatMap 的区别

    2023-12-06 11:20:05       57 阅读
  2. 麒麟v10 数据盘初始化 gpt分区

    2023-12-06 11:20:05       96 阅读
  3. golang使用sip实现语音通话

    2023-12-06 11:20:05       55 阅读
  4. LightDB - 支持 last_day 函数[mysql兼容]

    2023-12-06 11:20:05       57 阅读
  5. NLP中几个简单的,字符串相似度计算方法

    2023-12-06 11:20:05       54 阅读
  6. AI:大语言模型LLM

    2023-12-06 11:20:05       60 阅读
  7. Pytest 的小例子

    2023-12-06 11:20:05       58 阅读
  8. css基础

    2023-12-06 11:20:05       58 阅读
  9. 什么是供应链金融分账系统?

    2023-12-06 11:20:05       59 阅读