0

So, I was reading some book and it had source code with a single cpp file, no classes and a bunch of static functions.

After some searches, I mostly see material about static member functions, which I know what they do and doesn't provide me any answer.

I also found something about anonymous namespaces vs static functions, but didn't quite understand the point.

So, can anybody out there provide me some insights on what are static non member functions, what's their uses or why to use them?

msmilkshake
  • 214
  • 1
  • 13
  • 1
    Static non-member functions are treated the same as non-member functions put in anonymous namespace. What does it do is explained [here](https://stackoverflow.com/questions/357404/why-are-unnamed-namespaces-used-and-what-are-their-benefits). – Fureeish Jul 17 '19 at 15:44
  • 4
    "_I also found something about anonymous namespaces vs static functions, but didn't quite understand the point._" Unless you explain what, exactly, was unclear about that, there's nothing stopping us in using the same terminology to explain it again, which would lead to you not understanding it again. So.. What, **exactly**, was unclear about all of the explanations you read? – Algirdas Preidžius Jul 17 '19 at 15:47

2 Answers2

0

One use of static free function is to prevent redeclaration of the function in different translation units which causes link errors.

Oblivion
  • 7,176
  • 2
  • 14
  • 33
0

What is the use of a static function

Static functions or more generally: functions with internal linkage are useful in encapsulating the function by preventing calls to it from other translation units than the one where it is defined.

It also helps prevent name collisions between functions defined and used in different translation units.

I also found something about anonymous namespaces vs static functions

Anonymous namespace are another, newer way of declaring functions with internal linkage. Unlike keyword static, they also allow definition of types with internal linkage.

eerorika
  • 232,697
  • 12
  • 197
  • 326