应该永远不要使用静态内联函数吗?
使用 inline
关键字有两个含义(第 7.1.3/4 节):
There are two implications of using the inline
keyword(§ 7.1.3/4):
- 它暗示编译器在调用点替换函数体比通常的函数调用机制更可取.
- 即使省略内联替换,也遵循内联的其他规则(尤其是 w.r.t 一个定义规则).
- It hints the compiler that substitution of function body at the point of call is preferable over the usual function call mechanism.
- Even if the inline substitution is omitted, the other rules(especially w.r.t One Definition Rule) for inline are followed.
如果需要,通常任何主流编译器都会在调用点替换函数体,因此不需要为#1
标记函数inline
.
Usually any mainstream compiler will substitute function body at the point of call if needed, so marking function inline
merely for #1
is not really needed.
进一步 w.r.t #2
,据我所知,当您将函数声明为 static inline
函数时,
Further w.r.t #2
, As I understand when you declare a function as static inline
function,
函数上的static
关键字强制inline
函数有一个内部链接(inline 函数有外部链接)这样的每个实例一个函数被视为一个单独的函数(每个函数的地址不同),这些函数的每个实例都有自己的静态局部变量副本&字符串文字(内联函数只有一个副本)
The static
keyword on the function forces the inline
function to have an internal linkage(inline functions have external linkage) Each instance of such a function is treated as a separate function(address of each function is different) and each instance of these functions have their own copies of static local variables & string literals(an inline function has only one copy of these)
因此这样的函数就像任何其他 static
函数一样,关键字 inline
不再重要,它变得多余了.
Thus such a function acts like any other static
function and the keyword inline
has no importance anymore, it becomes redundant.
因此,实际上标记一个函数 static
和 inline
都没有任何用处.它应该是static
(不是最喜欢的)或inline
(最喜欢的),
那么,在函数上同时使用 static
和 inline
是否实际上没用?
So, Practically marking a function static
and inline
both has no use at all. Either it should be static
(not most preferred) or inline
(most preferred),
So, Is using static
and inline
together on a function practically useless?
推荐答案
您的分析是正确的,但并不一定意味着无用.即使大多数编译器自动执行内联函数(原因 #1),最好声明 inline
只是为了描述意图.
Your analysis is correct, but doesn't necessarily imply uselessness. Even if most compilers do automatically inline functions (reason #1), it's best to declare inline
just to describe intent.
不考虑与 inline
的交互,应该谨慎使用 static
函数.命名空间范围内的 static
修饰符以前被弃用,取而代之的是未命名的命名空间(C++03 §D.2).由于一些模糊的原因,我不记得它在 C++11 中被弃用了,但你应该很少需要它.
Disregarding interaction with inline
, static
functions should be used sparingly. The static
modifier at namespace scope was formerly deprecated in favor of unnamed namespaces (C++03 §D.2). For some obscure reason that I can't recall it was removed from deprecation in C++11 but you should seldom need it.
因此,实际上将函数标记为静态和内联根本没有用.它应该是静态的(不是最喜欢的)或内联的(最喜欢的),
So, Practically marking a function static and inline both has no use at all. Either it should be static(not most preferred) or inline(most preferred),
没有偏好的概念.static
意味着具有相同签名的不同函数可能存在于不同的 .cpp
文件(翻译单元)中.inline
没有 static
意味着不同的翻译单元可以用相同的定义定义相同的函数.
There's no notion of preference. static
implies that different functions with the same signature may exist in different .cpp
files (translation units). inline
without static
means that it's OK for different translation units to define the same function with identical definitions.
是首选的是使用未命名的命名空间而不是static
:
What is preferred is to use an unnamed namespace instead of static
:
namespace {
inline void better(); // give the function a unique name
}
static inline void worse(); // kludge the linker to allowing duplicates
相关文章