C语言 为什么将 C 函数声明为静态内联函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21835664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why declare a C function as static inline?
提问by iamjustaprogrammer
I came across an example for a C-function declared as:
我遇到了一个声明为 C 函数的示例:
static inline CGPoint SOCGPointAdd(const CGPoint a, const CGPoint b) {
return CGPointMake(a.x + b.x, a.y + b.y);
}
Until now, I declared utility C-functions in .h files and implemented them in .m files, just like this:
到目前为止,我在 .h 文件中声明了实用 C 函数并在 .m 文件中实现了它们,就像这样:
CGPoint SOCGPointAdd(const CGPoint a, const CGPoint b) {
return CGPointMake(a.x + b.x, a.y + b.y);
}
I can use this function "inline" anywhere I want and it should also be "static" because it's not associated with any object, like an Objective-c method. What is the point / advantage of specifying "static" and "inline"?
我可以在任何我想要的地方“内联”使用这个函数,它也应该是“静态的”,因为它不与任何对象相关联,比如 Objective-c 方法。指定“静态”和“内联”的要点/优势是什么?
回答by Eric Postpischil
inlinedoes not mean you can use the function “inline” (it is normal to use functions inside other functions; you do not need inlinefor that); it encourages the compiler to build the function into the code where it is used (generally with the goal of improving execution speed).
inline并不意味着您可以使用“内联”函数(在其他函数中使用函数是正常的;您不需要这样做inline);它鼓励编译器将函数构建到使用它的代码中(通常是为了提高执行速度)。
staticmeans the function name is not externally linked. If the function were not declared static, the compiler is required to make it externally visible, so that it can be linked with other object modules. To do this, the compiler must include a separate non-inline instance of the function. By declaring the function static, you are permitting all instances of it to be inlined in the current module, possibly leaving no separate instance.
static表示函数名称没有外部链接。如果函数没有被声明static,编译器需要让它在外部可见,以便它可以与其他目标模块链接。为此,编译器必须包含一个单独的非内联函数实例。通过声明 function static,您允许它的所有实例内联在当前模块中,可能不会留下单独的实例。
static inlineis usually used with small functions that are better done in the calling routine than by using a call mechanism, simply because they are so short and fast that actually doing them is better than calling a separate copy. E.g.:
static inline通常与在调用例程中比使用调用机制更好地完成的小函数一起使用,仅仅是因为它们如此短而快速,以至于实际执行它们比调用单独的副本更好。例如:
static inline double square(double x) { return x*x; }
回答by Merlevede
If the storage class is extern, the identifier has external linkage and the inline definition also provides the external definition. If the storage class is static, the identifier has internal linkage and the inline definition is invisible in other translation units.
如果存储类是 extern,则标识符具有外部链接,内联定义也提供外部定义。如果存储类是static,则标识符具有内部链接,并且内联定义在其他翻译单元中不可见。
By declaring a function inline, you can direct the compiler to integrate that function's code into the code for its callers (to replace the complete code of that function directly into the place from where it was called). This makes execution faster by eliminating the function-call overhead. That's why inline functions should be very short.
通过声明一个函数inline,您可以指示编译器将该函数的代码集成到其调用者的代码中(将该函数的完整代码直接替换到调用它的地方)。这通过消除函数调用开销使执行速度更快。这就是内联函数应该非常短的原因。
回答by Mayank Agarwal
Inline functions are for defining in header files.Small functions are defined in header files. It should be static so that it can acess only static members.
内联函数用于在头文件中定义。小函数在头文件中定义。它应该是静态的,以便它只能访问静态成员。

