我什么时候应该在非成员函数之前写关键字'static'? [英] When should I write the keyword 'static' before a non-member function?

查看:15
本文介绍了我什么时候应该在非成员函数之前写关键字'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.

这篇关于我什么时候应该在非成员函数之前写关键字'static'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆