开发者

Inline functions with internal linkage?

In C: Why is so that only inline functions with internal linkage (ie declared with static) may reference (ie copy address, read, write, or call) a variable or function at file scope开发者_StackOverflow中文版 with static storage duration while other inline functions may not?


This is how things are defined.

The inlined function would be inserted in the module where it is called. So, it can't access the private stuff in its module where it is defined.

If the inlined function is only used in that module(internal linkage). Then it is safe to grant it an access to the "private" stuff of that module.


An object or function with file scope, declared with the storage specifier "static" has internal linkage. While its lifetime is the entire execution of the program, a object with internal linkage is not declared for (i.e visible from) other translation units.

For an inline function with external linkage, the compiler may:

  • call the instance within the translation unit in which the function is declared
  • perform an inline substitution at the point of the call
  • create an identical but separate instance of the function within the calling translation unit

In the last two cases, objects with internal linkage would not be visible. Therefore an inline function with external linkage cannot reference an identifier with internal linkage.

Furthermore, it "shall not contain a definition of a modifiable object with static storage duration" as this could result in multiple instances of that object which is probably not the intended behavior.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜