문제

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 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top