简体   繁体   中英

Local static/thread_local variables of inline functions?

If I have a static local variable or thread_local local variable that is within an inline function that is defined in different translation units, in the final program are they guaranteed by the standard to have the same address?

// TU1:
inline int* f() { static int x; return &x; }
extern int* a;
void sa() { a = f(); }

// TU2:
inline int* f() { static int x; return &x; }
extern int* b;
void sb() { b = f(); }

// TU3:
int *a, *b;
void sa();
void sb();
int main() { sa(); sb(); return a == b; }

Will the above always return 1?

Yes, it's always the same object. By [dcl.fct.spec]/4:

An inline function with external linkage shall have the same address in all translation units. A static local variable in an extern inline function always refers to the same object. A type defined within the body of an extern inline function is the same type in every translation unit.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM