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

查看:207
本文介绍了我应该在什么时候在非成员函数之前写关键字'static'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近看到一些关于 static 关键字的函数,我想知道如何正确使用它。



1)我应该在非成员函数之前编写关键字 static



2)在标题中定义静态非成员函数很危险吗?为什么(不是)?







3)是否可以以某种方式在头文件中定义一个类,以便它只能在首先使用它的翻译单元中可用?



(我问这是因为我正在学习STL,它可能是一个很好的解决方案,我的谓词等(可能是函子)因为我不喜欢在cpp文件中定义非成员函数的函数)



(另外,我认为它是相关的一个方式到原始问题,因为根据我目前的推理,它会做一个事情, static 之前的函数)



EDIT



在看到一些答案时出现的另一个问题:



strong> 4)很多人告诉我,我必须在头文件中声明静态函数,并在源文件中定义它。但是静态函数对于翻译单元是唯一的。链接器如何能够知道 翻译单位是否是唯一的,因为标题文件与源文件没有直接关系(仅当您包含它们时)?

解决方案

static ,我认为你使用它,是一种符号隐藏。声明 static 的函数没有给出全局可见性(类似于Unix的 nm 将显示为't' T')。这些函数不能从其他翻译单元调用。



对于C ++, static 或者更少,通过匿名命名空间,例如

  static int x = 0; 

非常等同于

  namespace {
int x = 0;
}

请注意,匿名命名空间对于每个编译单元都是唯一的。



static 不同,匿名命名空间也适用于类。您可以说

  namespace {
class Foo {};
}

并在其他翻译单元中重用该类名称。我认为这是你的观点3。



编译器实际上给每个符号定义了一个唯一的名称(我认为它包括编译时间)。这些符号从不可用于另一个翻译单元,并且永远不会与另一个翻译单元的符号冲突。



请注意,所有非成员函数声明为 inline 也默认为 static 。这是 static 的最常见(和隐含)的使用。至于第2点,在标题中定义 static 但不是 inline 函数是一个漂亮的情况:它不危险本身,但它是如此罕见的用途,它可能会令人困惑。这样的函数可以或可以不在每个翻译单元中发出。如果你从来没有实际调用一些TU中的函数,编译器可能会生成警告。如果静态函数里面有一个静态变量,你会得到一个单独的变量每个翻译单位,即使一个定义在一个单一的 .h 这可能是混乱。



对于第4点,我怀疑这些人正在混合静态成员函数的含义 static 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) When should I write the keyword static before a non-member function?

2) Is it dangerous to define a static non-member function in the header? Why (not)?


(Side Question)

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?

(The reason that I'm asking this is because I'm learning STL and it might be a good solution for my predicates etc (possibly functors), since I don't like to define functions other than member-functions in the cpp file)

(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)

EDIT

Another question that came up while seeing some answers:

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, 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.

For C++, static in this sense has been replaced, more or less, by the anonymous namespace, e.g.,

static int x = 0;

is pretty equivalent to

namespace {
  int x = 0;
}

Note that the anonymous namespace is unique for every compilation unit.

Unlike static, the anonymous namespace also works for classes. You can say something like

namespace {
 class Foo{};
}

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.

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.

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天全站免登陆