命名空间
变体
操作

_Noreturn 函数说明符 (自 C11 起)(在 C23 中已弃用)

来自 cppreference.cn
< c‎ | language

指定函数不会返回到其调用点。

目录

[编辑] 语法

_Noreturn function_declaration (自 C11 起)(在 C23 中已弃用)

[编辑] 解释

_Noreturn 关键字出现在函数声明中,并指定函数不会通过执行 return 语句或到达函数体末尾来返回(它可以通过执行 longjmp 返回)。如果声明为 _Noreturn 的函数返回,则行为是未定义的。如果可以检测到这种情况,建议编译器发出诊断信息。

_Noreturn 说明符可以在同一个函数声明中出现多次,其行为与出现一次相同。

此说明符通常通过便利宏 noreturn 使用,该宏在头文件 <stdnoreturn.h> 中提供。

_Noreturn 函数说明符已弃用。应使用 [[noreturn]] 属性来代替。

noreturn 也已弃用。

(自 C23 起)

[编辑] 关键字

_Noreturn

[编辑] 标准库

以下函数在标准库中是 noreturn 的

[编辑] 示例

#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
 
// causes undefined behavior if i <= 0
// exits if i > 0
noreturn void exit_now(int i) // or _Noreturn void exit_now(int i)
{
    if (i > 0)
        exit(i);
}
 
int main(void)
{
    puts("Preparing to exit...");
    exit_now(2);
    puts("This code is never executed.");
}

输出

Preparing to exit...

[编辑] 参考

  • C23 标准 (ISO/IEC 9899:2024)
  • 6.7.4 函数说明符 (页码: 待定)
  • 7.23 _Noreturn <stdnoreturn.h> (页码: 待定)
  • C17 标准 (ISO/IEC 9899:2018)
  • 6.7.4 函数说明符 (页码: 90-91)
  • 7.23 _Noreturn <stdnoreturn.h> (页码: 263)
  • C11 标准 (ISO/IEC 9899:2011)
  • 6.7.4 函数说明符 (页码: 125-127)
  • 7.23 _Noreturn <stdnoreturn.h> (页码: 361)

[编辑] 参见

[[noreturn]](C23)[[_Noreturn]](C23)(已弃用)
{{{notes}}}
指示函数不会返回
(属性说明符)[编辑]
[[noreturn]] 的 C++ 文档