What is a “static” function in C?

前端 未结 12 1048
野的像风
野的像风 2020-11-22 16:03

The question was about plain c functions, not c++ static methods, as clarified in comments.

I understand what a static variable is, but wha

12条回答
  •  死守一世寂寞
    2020-11-22 16:16

    "What is a “static” function in C?"

    Let's start at the beginning.

    It´s all based upon a thing called "linkage":

    "An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage. 29)There are three kinds of linkage: external, internal, and none."

    Source: C18, 6.2.2/1


    "In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function. Each declaration of an identifier with no linkage denotes a unique entity."

    Source: C18, 6.2.2/2


    If a function is defined without a storage-class specifier, the function has external linkage by default:

    "If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern."

    Source: C18, 6.2.2/5

    That means that - if your program is contained of several translation units/source files (.c or .cpp) - the function is visible in all translation units/source files your program has.

    This can be a problem in some cases. What if you want to use f.e. two different function (definitions), but with the same function name in two different contexts (actually the file-context).

    In C and C++, the static storage-class qualifier applied to a function at file scope (not a static member function of a class in C++ or a function within another block) now comes to help and signifies that the respective function is only visible inside of the translation unit/source file it was defined in and not in the other TLUs/files.

    "If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage. 30)"


    1. A function declaration can contain the storage-class specifier static only if it is at file scope; see 6.7.1.

    Source: C18, 6.2.2/3


    Thus, A static function only makes sense, iff:

    1. Your program is contained of several translation units/source files (.c or .cpp).

    and

    1. You want to limit the scope of a function to the file, in which the specific function is defined.

    If not both of these requirements match, you don't need to wrap your head around about qualifying a function as static.


    Side Notes:

    • As already mentioned, A static function has absolutely no difference at all between C and C++, as this is a feature C++ inherited from C.

    It does not matter that in the C++ community, there is a heartbreaking debate about the depreciation of qualifying functions as static in comparison to the use of unnamed namespaces instead, first initialized by a misplaced paragraph in the C++03 standard, declaring the use of static functions as deprecated which soon was revised by the committee itself and removed in C++11.

    This was subject to various SO questions:

    Unnamed/anonymous namespaces vs. static functions

    Superiority of unnamed namespace over static?

    Why an unnamed namespace is a "superior" alternative to static?

    Deprecation of the static keyword... no more?

    In fact, it is not deprecated per C++ standard yet. Thus, the use of static functions is still legit. Even if unnamed namespaces have advantages, the discussion about using or not using static functions in C++ is subject to one´s one mind (opinion-based) and with that not suitable for this website.

提交回复
热议问题