我什么时候应该在非成员函数之前写关键字“static"?
我最近在 SO 上看到了一些关于函数前 static
关键字的内容,我想知道如何正确使用它.
I've recently seen a bit on SO about the static
keyword before a function and I'm wondering how to use it properly.
1) 什么时候应该在非成员函数前写关键字static
?
1) When should I write the keyword static
before a non-member function?
2) 在标头中定义静态非成员函数是否危险?为什么(不是)?
2) Is it dangerous to define a static non-member function in the header? Why (not)?
(辅助问题)
3) 是否可以以某种方式在头文件中定义一个类,以便它只能在您首先使用它的翻译单元中可用?
3) Is it possible to define a class in the header file in a certain way, so that it would only be available in the translation unit where you use it first?
(我问这个的原因是因为我正在学习 STL,它可能是我的谓词等(可能是函子)的一个很好的解决方案,因为我不喜欢定义成员以外的函数-cpp 文件中的函数)
(另外,我认为它与原始问题在某种程度上有关,因为根据我目前的推理,它会在函数执行之前执行与 static
相同的操作)
(Also, I think it is related in a way to the original question because according to my current reasoning, it would do the same thing as static
before a function does)
看到一些答案时出现的另一个问题:
Another question that came up while seeing some answers:
4) 很多人告诉我必须在头文件中声明静态函数,并在源文件中定义它.但是静态函数是翻译单元独有的.链接器如何知道哪个翻译单元是独一无二的,因为头文件不直接与源文件相关(仅当您包含它们时)?
4) Many people tell me I have to declare the static function in the header, and define it in the source file. But the static function is unique to the translation unit. How can the linker know which translation unit it is unique to, since header files do not directly relate to a source file (only when you include them)?
推荐答案
static
,我认为您正在使用它,是一种符号隐藏的方法.声明为 static
的函数不具有全局可见性(类 Unix 的 nm
会将这些显示为 't' 而不是 'T').其他翻译单元无法调用这些函数.
static
, as I think you're using it, is a means of symbol hiding. Functions declared static
are not given global visibility (a Unix-like nm
will show these as 't' rather than 'T'). These functions cannot be called from other translation units.
对于 C++,在这个意义上的 static
已或多或少被匿名命名空间取代,例如,
For C++, static
in this sense has been replaced, more or less, by the anonymous namespace, e.g.,
static int x = 0;
非常等同于
namespace {
int x = 0;
}
请注意,匿名命名空间对于每个编译单元都是唯一的.
Note that the anonymous namespace is unique for every compilation unit.
与 static
不同,匿名命名空间也适用于类.你可以这样说
Unlike static
, the anonymous namespace also works for classes. You can say something like
namespace {
class Foo{};
}
并将该类名重用于其他翻译单元中的不相关类.我认为这符合您的第 3 点.
and reuse that class name for unrelated classes in other translation units. I think this goes to your point 3.
编译器实际上给你这样定义的每个符号一个唯一的名字(我认为它包括编译时间).这些符号永远不会被其他翻译单元使用,也永远不会与来自其他翻译单元的符号发生冲突.
The compiler actually gives each of the symbols you define this way a unique name (I think it includes the compilation time). These symbols are never available to another translation unit and will never collide with a symbol from another translation unit.
请注意,所有声明为 inline
的非成员函数默认也是 static
.这是 static
最常见(和隐含)的用法.至于第 2 点,在标头中定义 static
而不是 inline
函数是一个非常极端的情况:它本身并不危险,但它很少有用,可能会令人困惑.这样的函数可能会或可能不会在每个翻译单元中发出.如果您从未在某些 TU 中实际调用过该函数,则编译器可能会生成警告.如果该静态函数内部有一个静态变量,即使在单个 .h
中有一个定义,每个翻译单元也会得到一个单独的变量,这可能会造成混淆.只是没有很多(非内联)用例.
Note that all non-member functions declared to be inline
are also by default static
. That's the most common (and implicit) use of static
. As to point 2, defining a static
but not inline
function in a header is a pretty corner case: it's not dangerous per se but it's so rarely useful it might be confusing. Such a function might or might not be emitted in every translation unit. A compiler might generate warnings if you never actually call the function in some TUs. And if that static function has within it a static variable, you get a separate variable per translation unit even with one definition in a single .h
which might be confusing. There just aren't many (non-inline) use cases.
关于第4点,我怀疑那些人将static
的静态成员函数含义与static
的链接含义混为一谈.这与为后者使用匿名命名空间的任何理由一样充分.
As to point 4, I suspect those people are conflating the static member function meaning of static
with that of the linkage meaning of static
. Which is as good a reason as any for using the anonymous namespace for the latter.
相关文章