标准对这种的定义就是UB。
有些情况下需要写这种不返回东西的函数,比如下面这个有人举了个需要直接abort而不返回任何东西的例子,来说明规范中为啥要定义成UB而不是强制为error。
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43943
如果确定自己的工程中绝对不应该出现这种写法的函数,就打开-Werror=return-type开关。
ISO_IEC_14882__2020-12.pdf 第160页:
8.7.4 The return statement
Flowing off the end of a constructor, a destructor, or a non-coroutine function with a cv void return type is equivalent to a return with no operand. Otherwise, flowing off the end of a function other than main (6.9.3.1) or a coroutine (9.5.4) results in undefined behavior.
翻译一下,就是:
ctor、dtor、void这三种函数中如果某个分支直到函数尾部都没有return语句的话,等价于这个函数分支是不带参数return了,不是UB。
上述三种函数之外的任何非main函数、非协程函数的函数中某个分支直到函数尾部都没有return语句的话,就是UB。
main函数、协程函数中的这种情况是单独有章节规定是否为UB的。
--
修改:z16166 FROM 61.48.129.*
FROM 61.48.129.*