使用现有的静态变量识别第一次调用?

Mik*_*ail 0 c++ gcc static-variables

有没有办法通过识别GCC用来促进静态变量(我的函数中已存在的变量)的内部(隐藏?)变量来识别函数是否第一次被调用?

我希望从C++代码中获取这些变量.

Mar*_*som 5

没有办法依赖编译器的内部,即使你尝试过,也不能保证它不会随着下一个版本而改变.

使用这个常见的习语:

static bool firsttime = true;
if (firsttime)
{
    firsttime = false;
    // other stuff here
}
Run Code Online (Sandbox Code Playgroud)