c++ - 在一个函数前面加[[noreturn]]是什么意思?

浏览:38日期:2023-03-24

问题描述

问题解答

回答1:

这是C++11的attribute specifier sequence( http://en.cppreference.com/w/... )

关于[[noreturn]],官方解释是

Indicates that the function does not return. This attribute applies to function declarations only. The behavior is undefined if the function with this attribute actually returns.

该specifier用来指示函数永不返回,有助于编译器进行编译优化(如尾递归等),也可以用于抑制编译器给出不必要的警告(如int f(); f();,不加[[noreturn]]的话,编译器会警告f()的返回值被忽略)

但是,若函数的确有返回值,而你却指定[[noreturn]]的话,这就是未定义行为了

相关文章: