Static functions outside classes

C++

C++ Problem Overview


Could someone tell me what's the purpose of declaring static functions outside classes? What's the difference between this 2? Are there any benefits for using static in this situation?

static void someRandomFunction();

int main()
{
    someRandomFunction();
    return 0;
}

and

void someRandomFunction();

int main()
{
    someRandomFunction();
    return 0;
}

C++ Solutions


Solution 1 - C++

At namespace scope, static gives a name internal linkage, meaning that it is only accessible within the translation unit that contains the definition. Without static, it has external linkage, and is accessible in any translation unit.

So you'd use static (or, alternatively, an unnamed namespace) when writing a function that's only intended for use within this unit; the internal linkage means that other units can define different functions with the same name without causing naming conflicts.

Non-static functions (and global names in general) are better declared in a header, to make sure that every translation unit that uses them gets the same declaration.

Solution 2 - C++

The static keyword on global functions or variables limits the visibility and linkage scope of the function or variable to the current translation unit.

That means that for a function, it can only be called from the current source file, and not from other source files.

Solution 3 - C++

A static function remains visible only in file scope. This is a C feature.
The recommended way to do it in C++ is using an anonymous namespace, as in:

namespace // no name, i.e. anonymous
{
   void someRandomFunction(); 
}

int main()
{
    someRandomFunction(); // visible only within this file.
    return 0;
}

Note that the function body also has to be declared somewhere within the same file since the linker will not try to find it in other (external) translation units.
So void someRandomFunction(); is really a forward declaration for a function that is defined elsewhere in the same file (i.e. in the same translation unit).

If the function is actually called, you will get a linking error unless the function body is defined in the same file.

(The more pedantic technical term is actually not file but translation-unit since the body might be in an #includeed header do not in the actual file per-se. )

Solution 4 - C++

Static methods and static functions are entirely different things.

Static methods are methods of a class instead of an instance (which you already know, as it seems).

Static functions, on the other hand, are function which are available only in the module they are defined in. They are not exported and cannot be put in a header file and used in another c file. This way you can write different functions sharing the same name, and also the compiler may optimize your code more thoroughly by inlining the function, knowing that no other file is dependant on it.

Solution 5 - C++

static void someRandomFunction();

This has to be used within same compilation unit (source file) and outside that compilation unit, not available for use. Whereas, if you have

void someRandomFunction();

with one definition acrosss the program, the function can be used by any compilation unit globally across the program

Solution 6 - C++

static tells the compiler not add the function to the symbol table for the object file. This effectively means that the linker is unable to find the function which in turn means you can only use the function directly in the current compilation unit. You can however call static functions from another compilation unit if this is done through a function pointer.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncuvView Question on Stackoverflow
Solution 1 - C++Mike SeymourView Answer on Stackoverflow
Solution 2 - C++Some programmer dudeView Answer on Stackoverflow
Solution 3 - C++Adi ShavitView Answer on Stackoverflow
Solution 4 - C++AdamView Answer on Stackoverflow
Solution 5 - C++Dr. Debasish JanaView Answer on Stackoverflow
Solution 6 - C++doronView Answer on Stackoverflow