简体   繁体   English

静态内联,外部内联和普通内联函数有什么区别?

[英]What's the difference between static inline, extern inline and a normal inline function?

What's the difference between a static inline , extern inline and a normal inline function? static inlineextern inline和常规inline函数有什么区别?

I've seen some vague explanations about this. 我已经看到了一些模糊的解释。 As far as I've understood, static inline is not just an inline function that is meant to only be referred to within a certain file as the static keyword usually means. 据我所知, static inline不仅仅是一个inline函数,它只能像static关键字通常所指的那样在某个文件中被引用。 The same goes for extern inline too I guess, it's not the same explanation as with extern variables. 我猜extern inline也是如此,这与extern变量的解释不同。 Any answers would be greatly appreciated! 任何答案将不胜感激!

A function definition with static inline defines an inline function with internal linkage. 具有static inline联的函数定义定义具有内部链接的内联函数。 Such function works "as expected" from the "usual" properties of these qualifiers: static gives it internal linkage and inline makes it inline. 此类函数从这些限定符的“常规”属性“按预期”起作用: static为其提供内部链接, inline使其为内联。 So, this function is "local" to a translation unit and inline in it. 因此,此功能对于翻译单元是“本地的”,并且在其中内联。

A function definition with just inline defines an inline function with external linkage. inline联的函数定义定义具有外部链接的内联函数。 However, such definition is referred to as inline definition and it does not work as external definition for that function. 但是,这种定义称为内联定义 ,并且不能用作该函数的外部定义 That means that even though this function has external linkage, it will be seen as undefined from other translation units, unless you provide a separate external definition for it somewhere. 这意味着即使该功能具有外部链接,也将被其他翻译单位视为未定义 ,除非您在某处为它提供了单独的外部定义

A function definition with extern inline defines an inline function with external linkage and at the same time this definition serves as external definition for this function. 具有extern inline联函数的函数定义定义具有外部链接的内联函数,与此同时,该定义也用作该函数的外部定义 It is possible to call such function from other translation units. 可以从其他翻译单元调用该函数。

The last two paragraphs mean that you have a choice of providing a single extern inline definition for an inline function with external linkage, or providing two separate definitions for it: one inline and other extern . 最后两段意味着您可以选择为具有外部链接的内联函数提供单个extern inline定义,或者为其提供两个单独的定义:一个inline和另一个extern In the latter case, when you call the function the compiler is allowed to chose either of the two definitions. 在后一种情况下,当您调用函数时,允许编译器选择两个定义之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM