I encountered an interesting case while reading others code.
In the head file, defined a static variable and an inline function are simplified as following:
static int ply;
inline int WTM(){return ply;}
and the function is called in some other cpp
file that include this head.
cout << ply << " " << WTM();
The strange thing is that at where this function is called, the variable ply
inside that inline function has different value from the same variable just before it outside the function.
The output is 0 1;
I checked all the file and both ply
and WTM()
just have this single definition.
After that I have changed the code to the following:
static int ply;
static inline int WTM(){return ply;}
The two value became the same.
My compiler is g++ (GCC) 4.4.7
with default setting.
I searched about this phenomenon and get to this two link:
Difference between an inline function and static inline function
and
http://gcc.gnu.org/onlinedocs/gcc/Inline.html
but still don't understand why this could happen (especially for why they could have different values in the first situation). I wonder if someone can tell me how the compiler will expand those two pieces of code (I tried using -E
but it seems not working on inline function).