DEV Community

Discussion on: 2 ways to use static with functions in C++

Collapse
 
pgradot profile image
Pierre Gradot • Edited

I am very surprised that you didn't know that we can use static to change the linkage. You can also use it with variables and get a static bool in a *.cpp file :D

Collapse
 
sandordargo profile image
Sandor Dargo

Somehow I never noticed it. Even in this case, it was only about hiding the existance of a helper method which I don't think was the best idea as it introduces positional coupling (does that exist?)...

Someone suggested to use anonymous namespaces instead as in addition "anonymous namespaces allow you to have internal type declarations, something that cannot be achieved through the static."

Collapse
 
pgradot profile image
Pierre Gradot

Anonymous namespaces indeed have the same purpose. After may years, I still prefer static: when you see a function prototype, you immediately know that it has internal linkage. With anonymous namespaces, you have to scroll up the file to see that (or your IDE's hints, if available) to check.