c++ - static variable in inline function -
this question has answer here:
- static variables in inlined function 9 answers
i'm interested in happens "under hood" when following inline function called in several translation units.
namespace some_name { inline const float& get_float() { static const float = 5.0f; return a; } }
my intention create externally linked variable 'a', can used across code (if header namespace included), wanted prevent change variable. testing seems succeeded, i'm interested in happens when call function first time , next several times.
additional question: polluting global namespace static variable declaration/definition?
but i'm interested in happens when call function first time , next several times.
the initialization static (doesn't depend on @ run-time), performed @ start of program. calls return reference static object. , calls expanded inline, , use static object directly.
a simpler option use global variable in namespace.
additional question: polluting global namespace static variable declaration/definition?
no. static variable local, doesn't pollute namespace. function "pollute" namespace declared.
Comments
Post a Comment