C固定inline函数重新定义规则 [英] C static inline function redefinition rules

查看:175
本文介绍了C固定inline函数重新定义规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设在 bar.h 可能的存在:

static inline int fun () { return 2; }

和确保乐趣始终定义 foo.h中包含以下内容:

And to ensure that fun is always defined foo.h contains the following:

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 ();
   /* ... */

与海湾合作委员会(4.0.1)(旧,但它是我目前拥有)的行为是按预期 - 酒吧版本丢失时foo的版本,使用时存在使用栏版本。

With gcc (4.0.1) (old, but it's what I have currently) the behavior is as expected - the foo version is used when the bar version is missing and the bar version is used when it exists.

推荐答案

没有,这是不允许的。的有趣的定义()声明乐趣()来有内部链接(由于静态),并在§6.9C标准说:

No, this is not allowed. Those definitions of fun() declare fun() to have internal linkage (due to the static), and §6.9 in the C standard says:

有不得超过一个
  对于每个外部定义
  标识符与内部声明
  联动在翻译单元。

There shall be no more than one external definition for each identifier declared with internal linkage in a translation unit.

一个应子句不确定的行为,这意味着你的程序的语义是完全不确定的,并且编译器没有发出错误消息的冲突。

Violation of a "shall" clause is undefined behaviour, which means that the semantics of your program are completely undefined, and the compiler doesn't have to issue an error message.

这篇关于C固定inline函数重新定义规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆