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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.