问题描述
谁能告诉我内联函数和静态内联函数有什么区别?
Can anybody tell me what the difference is between an inline function and static inline function?
在哪些情况下我应该更喜欢静态内联而不是内联?
In which cases should I prefer static inline over inline?
我问这个问题是因为我有一个内联函数,我在链接过程中遇到了编译问题(重定位错误:...符号已被丢弃部分丢弃...
).我使它成为一个正常的功能并且它起作用了.现在我的一些前辈告诉我尝试使用静态内联.以下是我的功能:
I am asking this question because I have an inline function for which I am facing compilation issues during linking (relocation error:... symbol has been discarded with discarded section ...
). I made it a normal function and it worked.
Now some of my seniors told me try with static inline.
Below is my function:
inline void wizSendNotifier (const char* nn_name, bpDU* arg=0, int aspect = -1)
{
wizuiNotifier* notifier = ::wizNtrKit.getNotifier (nn_name);
notifier->notify (arg, aspect);
}
而这不是在课堂内.这是在一个头文件中!
and this not inside a class. This is inside a header file!
我想对静态函数的调用应该只在定义它的特定 TU 中完成.
I guess the call to a static function should be done only in the particular TU where it is defined.
由于我的函数在头文件中,如果我将其设为静态,那么在我包含该头文件的地方是否会在该翻译单元中使用静态函数?
Since my function is in a header file and if i make it static, will it be the case that where ever I include that header file the static function can used used in that translation unit?
推荐答案
非静态inline
函数声明在使用它的每个翻译单元(源文件)中引用相同的函数.
The non-static inline
function declaration refers to the same function in every translation unit (source file) that uses it.
单一定义规则要求函数定义的主体在包含它的每个 TU 中都是相同的,并且有一个较长的相同"定义.如果源文件都使用相同的标头,并且该函数不使用任何具有内部链接的全局名称(包括 static
函数)或任何在不同的 TU.
The One Definition Rule requires that the body of the function definition is identical in every TU that contains it, with a longish definition of "identical". This is usually satisfied provided that the source files all use the same header, and provided that the function doesn't use any global names with internal linkage (including static
functions) or any macros that are defined differently in different TUs.
我不记得以前遇到过那个特定的链接器错误,但至少有可能是这些限制之一造成的.您有责任满足这些要求:未定义的行为,如果您不满足,则无需诊断.
I don't remember encountering that particular linker error before, but it's at least possible that one of these restrictions is responsible. It's your responsibility to satisfy the requirements: undefined behavior with no diagnostic required if you don't.
static inline
函数声明指的是每个翻译单元中的不同函数,恰好具有相同的名称.它可以使用在不同 TU 中不同的 static
全局名称或宏,在这种情况下,该函数在不同 TU 中的行为可能不同,即使其在头文件中的定义看起来相同".
The static inline
function declaration refers to a different function in each translation unit, that just so happens to have the same name. It can use static
global names or macros that are different in different TUs, in which case the function might behave differently in the different TUs, even though its definition in the header file "looks the same".
由于这种差异,如果函数包含任何static
局部变量,那么它的行为会根据它是否为static
而有所不同.如果它是 static
,那么每个 TU 都有自己的函数版本,因此有自己的 static
局部变量副本.如果它是 inline
only,那么所有 TU 使用的 static
局部变量只有一个副本.
Because of this difference, if the function contains any static
local variables then it behaves differently according to whether it is static
or not. If it is static
then each TU has its own version of the function and hence its own copy of the static
local variables. If it's inline
only, then there is only one copy of the static
local variables used by all TUs.
这篇关于内联函数和静态内联函数的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!