【发布时间】:2011-06-23 01:04:53
【问题描述】:
假设在bar.h 中可能存在:
static inline int fun () { return 2; }
并确保fun 始终被定义foo.h 包含以下内容:
static inline int fun () { return 3; }
当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 ();
/* ... */
使用 gcc (4.0.1)(旧的,但这是我目前所拥有的)行为符合预期 - 当 bar 版本丢失时使用 foo 版本,而 bar 版本在存在时使用。
【问题讨论】:
-
我应该指出
void func()和void func(void)在 C 语言中的细微差别。 -
@Chris 您是否建议我可以通过将显式
void添加到签名中的两个定义之一来避免未定义的行为? -
不,C 不允许重载。看起来你打算
int fun(void)比int fun()更准确。
标签: c function static shadow undefined-behavior