ezp*_*zpz 2 c static function shadow undefined-behavior
假设在bar.h有可能存在:
static inline int fun () { return 2; }
Run Code Online (Sandbox Code Playgroud)
并确保fun始终定义foo.h包含以下内容:
static inline int fun () { return 3; }
Run Code Online (Sandbox Code Playgroud)
bar.h包含定义时,以下是否会引发未定义的行为?
#include "foo.h" /* ensure there is always a definition */
#include "bar.h" /* use the bar definition if it exists */
int main () {
/* ... */
int x = fun ();
/* ... */
Run Code Online (Sandbox Code Playgroud)
使用gcc(4.0.1)(旧的,但它是我目前所拥有的)行为是预期的 - 当缺少条形版本时使用foo版本,并且当它存在时使用条形图版本.
不,这是不允许的.fun()声明这些定义fun()具有内部联系(由于static),而C标准中的§6.9表示:
对于在翻译单元中使用内部链接声明的每个标识符,只能有一个外部定义.
违反"shall"子句是未定义的行为,这意味着程序的语义完全未定义,编译器不必发出错误消息.