c++ - static variable in inline function -


this question has answer here:

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

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -