static and inline

风流意气都作罢 提交于 2019-12-06 15:19:18
  1. A static function can't be used in other translation units. That's their raison d'être.

  2. inline hints to the compiler that the function should be inlined instead of called.

  3. Making a function static is different than using a macro. A macro essentially overrules the compiler. Whether it thinks it wise to inline or not, a macro will be inlined; macros are textual substitution. You can also pass a static function to something requiring a function pointer. Can't do that with a macro.

  4. Macros will forcefully inline anything. Even specifying inline can be overruled.

  5. Make functions you don't want to export static. If a function is really small, and you really think it should be inlined, you can tell the compiler that with inline. Macros are really only for metaprogramming. The compiler knows better than you.

static and inline serve two very different purposes.

As you say correctly static means that the symbol of the function is not exported from the compilation unit where it is defined. Therefore different compilation units can have such symbols with the same name without conflict. Whether or not this corresponds to the same function declaration and definition is up to you. But such functions as all statically allocated objects can well be used in a different compilation unit by statically or dynamically exporting a pointer to it.

inline is different. Its intent is to make it possible for the compiler to inline your function, thus the name, but its major direct effect is that the function symbol is usually not emitted at all. This is designed for the purpose that you may put the definition of the function in a header file and include that file in several compilation units without creating multiple symbols in each of them. For the defined function it also has the effect that you are not allowed to declare static variables inside an inline function, since it would not be clear at all in which compilation unit that object would have to be realized.

So to summarize, static generates plenty of copies of your function, inline generates none; static has its major use in ".c" files and inline in ".h"

The first has the effect that you may at certain places not detect that two function pointers point to the "same" function, the second may have the effect that if you need a function pointer to the function there is no function object to which it would refer. Such a function can be forced to be emitted (in just one compilation unit!) by placing a sort of "instantiation" in the .c file:

// .h definition
inline void toto(void) { }

// .c instantiation
void toto(void);

These days compilers will determine whether an inline function will actually be inlined or not, as not all functions are good candidates for it. If so, then function body is simply injected/inlined where the respective function is referenced.

Such functions should be reserved for frequent function calls where the function body is usually quite short, though i suppose this doesnt have to be the case.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!