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

时间:2021-11-04 21:24:00

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

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

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. The same goes for extern inline too I guess, it's not the same explanation as with extern variables. Any answers would be greatly appreciated!

我已经看到了一些模糊的解释。据我所知,静态内联不仅仅是一个内联函数,只能在某个文件中引用,因为静态关键字通常意味着。外部内联也是如此,我想,这与外部变量的解释不一样。任何答案将不胜感激!

1 个解决方案

#1


18  

A function definition with static inline defines an inline function with internal linkage. Such function works "as expected" from the "usual" properties of these qualifiers: static gives it internal linkage and inline makes it 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. 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. It is possible to call such function from other translation units.

具有extern内联的函数定义定义了具有外部链接的内联函数,同时该定义用作此函数的外部定义。可以从其他翻译单元调用此类功能。

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. In the latter case, when you call the function the compiler is allowed to chose either of the two definitions.

最后两段意味着您可以选择为具有外部链接的内联函数提供单个外部内联定义,或者为其提供两个单独的定义:一个内联和另一个外部。在后一种情况下,当您调用该函数时,允许编译器选择两个定义中的任何一个。

#1


18  

A function definition with static inline defines an inline function with internal linkage. Such function works "as expected" from the "usual" properties of these qualifiers: static gives it internal linkage and inline makes it 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. 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. It is possible to call such function from other translation units.

具有extern内联的函数定义定义了具有外部链接的内联函数,同时该定义用作此函数的外部定义。可以从其他翻译单元调用此类功能。

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. In the latter case, when you call the function the compiler is allowed to chose either of the two definitions.

最后两段意味着您可以选择为具有外部链接的内联函数提供单个外部内联定义,或者为其提供两个单独的定义:一个内联和另一个外部。在后一种情况下,当您调用该函数时,允许编译器选择两个定义中的任何一个。