Search code examples
c++scopestatic-functions

C++ file-scope static functions


When should I consider using static functions defined at file scope?

I typically use them when the task done in one such function doesn't really belong in member functions of any class and when such a task is only needed (repeatedly) in a certain source file.

Is my usage in line with why this feature exists? Or am I hijacking a concept that's intended for something else?


Solution

  • That's a perfectly valid use of file-scope static functions, but keep in mind that this usage of static has been deprecated for quite some time. It's generally preferred to use anonymous namespaces instead.