I know from C, that the static
qualifier on functions limits their visibility to the current translation unit. In C I use static, if I define a function in e.g. a *.c
file, which is intended for use wihtin that file only (mostly specialized helper functions).
I did not find any information on how this is treated in C++. I only found information on how to apply static
to member functions. But in my case, there is no class involved.
As far as I tested it, using static
on functions in c++ does not produce any compiler warnings and works just fine. Is it acceptable to use it in the same way as in C as stated above? Is it common practice or bad practice?
Asked
Active
Viewed 51 times
2

Richard Neumann
- 2,986
- 2
- 25
- 50
-
Recommended reading: https://en.cppreference.com/w/cpp/keyword/static – Casey May 01 '21 at 10:03
-
@Casey The cases listed under *Usage* there do not cover plain functions. However, the language seems to allow it. – Richard Neumann May 01 '21 at 10:23
-
@RichardNeumann: It’s the first one, about namespace members. – Davis Herring May 01 '21 at 10:25
-
Section (B) of [this answer](https://stackoverflow.com/a/43442151/3515670) answered my question. tl;dr: Yes, it's the same as in C. – Richard Neumann May 01 '21 at 10:28