更新时间:2022-06-28 20:34:25
第三种情况会在无命名空间中允许类定义中并未导出的声明 (Use (3) is somewhat discouraged in favor of unnamed namespaces that also allows for un-exported class declarations.)
第二种情况和类或实例无关,只是一个函数所拥有的变量,而且仅有一份。
#include<stdio.h> template<int num> void bar() { static int baz; printf("bar<%i>::baz = %i\n", num, baz++); } int main() { bar<1>(); // Output will be 0 bar<2>(); // Output will be 0 bar<3>(); // Output will be 0 bar<1>(); // Output will be 1 bar<2>(); // Output will be 1 bar<3>(); // Output will be 1 bar<1>(); // Output will be 2 bar<2>(); // Output will be 2 bar<3>(); // Output will be 2 return 0; }