Static Local Variable of a Template `inline` Function

static local variables of an inline function in C++ are guaranteed to exist as if being a single global variable, if my understanding is correct.

Does the same apply if the inline function is a template, where the compiler can generate multiple versions of the function?


The following article should answer you question very well: http://www.geeksforgeeks.org/templates-and-static-variables-in-c/

In short: The Compiler produces one static variable for each template.

If you want to have the same variable for all templates you can maybe try something like this:

int& hack()
{
  static int i = 10;
  return i;
}

template <typename T>
void fun(const T& x)
{
  int &i = hack();
  cout << ++i;
  return;
}
链接地址: http://www.djcxy.com/p/85486.html

上一篇: 使用ti.storekit购买应用程序

下一篇: 模板内联函数的静态局部变量