What is a function with keyword static
?
static
has different effects depending on its usage and context. It can be found in: function definition, global and local variables, data members, and member functions. As the question addresses the use of static
in functions, the scope of the answer remains the same.
Note : I'm only considering c ++ to elaborate the answer. Some of the key concepts remain the same in c (such as linking names), but I advise you to look for differences.
a static
function how does it work?
To understand this, we need to know what link means in English) in c ++ .
A name that refers to an object, reference, function, type, template, namespace, value, or any other name found in [dcl.dcl] / declaration , you can have link (linkage being the English term). If a name has binding, then it refers to the same entity that the name introduces into a statement at some other scope. If a name (function, variable etc) is declared in multiple scopes but does not have a sufficient link between them, then multiple instances of the same entity are generated. There are some connection types, such as no link (in the linkage), internal linkage, and external linkage .
Second [basic.link] / 2 :
- When a name has external link , the entity denoted by the name can be accessed in other Translation Units (read informally as other .cpp files). Functions declared in one UT and used in another is an example of such name (function name) and entity (function itself).
- When a name has internal link , the entity denoted by the name can not be accessed by other UTs, but can be accessed by other scopes within the UT where the name was declared. Remember this in particular because this case is involved with the effect of
static
on a function declaration.
- When a name has no connection, or no connection , the entity denoted by the name can not be accessed or out of the scope where it is declared. Local variables are an example, where more scopes can access it, but scopes do not.
If you continue reading to [basic.link] /3.1 , you will find the following: / p>
A name having namespace scope has internal linkage if it is the name of
3.1 a variable, function or function template that is explicitly declared static
; or, [...]
My emphases.
Translating freely, we have that any name with a namespace scope will have internal binding if the name of a function variable, function, or template is explicitly declared with static
.
Finally, answering the question, a function declared with static
causes its name to have an internal connection. That is, the function can only be accessed by its name within the UT in which it is declared (ie, informally, only within the .cpp file where it resides). Some effects of this are:
- Ensures that the function can only be used within the UT where it resides;
- Consequently, your name will not conflict with names of scopes outside UT;
- As this has to do with binding, the linker (the final program that calculates the binary symbols and binds them) will have less work because the function name is not exported;
- It gives more optimization possibilities for compilers, since the function can not be used outside UT, it can be fully optimized.
We can see an example of the last two items here .
When should you use a static
function?
One of its uses is to maintain the encapsulation of a feature that an UT provides, where the details of how functionality is implemented does not interest the user interface. Only the functions exposed for use would have external binding, while the implementation details remain internally linked.