c++ - [C++]带有 static 关键字的函数模板

标签 c++ templates

来自 google v8 js 引擎的代码,allocation.h:

 template <typename T>
 static void DeleteArray(T* array) {
  delete[] array;
}

这是一个函数模板(顶级,不在任何类中)。 但是 static 关键字有什么用呢?

最佳答案

它是一个模板实际上是一个附带问题。

它是静态的意味着该函数在文件外部不可见(但由于它位于 header 中,并且 header 实际上是包含它们的文件的一部分,这意味着包含 header 的文件外部;并且每个文件包含 header 实际上有其自己的、相同但私有(private)的函数版本)。

来自http://msdn.microsoft.com/en-us/library/s1sb61xd%28VS.80%29.aspx :

"When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared)."

请参阅此处,了解有关将其包含在头文件中的含义的更多信息:

C/C++: Static function in header file, what does it mean?

关于c++ - [C++]带有 static 关键字的函数模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4348642/

相关文章:

c++ - msys2 和 header 在 'wrong' 位置

c++ - OpenACC - 私有(private)二维数组

c++ - 如何将 boost::iostreams::mapped_file_source 与 gzip 压缩的输入文件一起使用

c++ - std::initializer_list,大括号初始化和 header

html - 用当前 CSS 替换新的 CSS Sprite

c++ - 我对模板类的显式实例化似乎不起作用

c++ - 有没有办法取消 GLFW 中的 60 fps 上限?

c++ - 嵌套模板类的前向声明

c++ - 当用字符串文字实例化时,模板 T & 参数到底是什么?

c++ - 使用 lambda 访问成员函数内的类模板参数类型失败