Reputation: 9785
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?
Upvotes: 7
Views: 4346
Reputation: 113
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:
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.
Upvotes: 3
Reputation: 96849
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.
Upvotes: 5