您的位置:首页 > 大数据 > 人工智能

inline static varaible

2016-07-28 14:33 381 查看
void doSomething()
{
static int value ;
}


You must realise that the static variable inside the function, simply put, a global variable hidden to all but the function's scope, meaning that only the function it is declared inside can reach it.

Inlining the function won't change anything:
inline void doSomething()
{
static int value ;
}


There will be only one hidden global variable. The fact the compiler will try to inline the code won't change the fact there is only one global hidden variable.

Now, if your function is declared static:
static void doSomething()
{
static int value ;
}


Then it is "private" for each compilation unit, meaning that every CPP file including the header where the static function is declared will have its own private copy of the function, including its own private copy of global hidden variable, thus as much variables
as there are compilation units including the header.

Adding "inline" to a "static" function with a "static" variable inside:
inline static void doSomething()
{
static int value ;
}


has the same result than not adding this "inline" keyword, as far as the static variable inside is concerned.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐