Can anyone explain why making function static in C++ makes it internally linked only. Is that just from plane standard or some language trick?
Asked
Active
Viewed 50 times
-3
-
2Best answer I have for this is "Because the C++ Standard says so." Someone decided the language needed this capability, the standardization body agreed, and the `static` keyword was added to the language. (actually, it was adopted from C, but the story in C is probably similar) Also look into [anonymous namespaces](https://en.cppreference.com/w/cpp/language/namespace#Unnamed_namespaces). – user4581301 Nov 29 '18 at 07:11
-
So you are saying that this regulation that static keyword makes variables invisible to external translation units comes from standard? – shota silagadze Nov 30 '18 at 10:39
-
Yes . Exactly. I don't have the section on hand, but it should be in the **[basic.link]** section of the C++ Standard document. A helpful search term to find it will be *Internal Linkage*. [Here is a link to a draft close to the C++11 Standard.](https://timsong-cpp.github.io/cppwp/n3337/) – user4581301 Nov 30 '18 at 20:00
1 Answers
0
static
can be confusing, because it has a few different meanings depending on context. In this context, it means that the variable being defined is visible only in the current translation unit:
int i; // visible in all translation units
static int j; // visible only in the current translation unit

Pete Becker
- 74,985
- 8
- 76
- 165
-
So you are saying that this regulation that static keyword makes variables invisible to external translation units comes from standard? – shota silagadze Nov 30 '18 at 10:39
-
1