(C++20) consteval立即函数

文章目录

  • 由来
  • consteval立即函数
    • 上下文的常量性质
    • lambda表达式
  • 编译期间确定
    • 无法获取函数指针
    • 查看汇编
  • END

由来

在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

你可能感兴趣的:(c++20,算法,c++)