Repository navigation
[SUGGESTION] Static: Keyword with many meanings #322
Description
In cpp, the keyword static is currently used at three different places:
- To declare objects with static lifetime
- To specify global variables with internal linkage
- To declare static members (data and functions) in classes
All of these 3 use cases are very different from one-another and therefore, using same keyword for all of them can be confusing, especially to beginners. Consider the following code:
static int a = 9;
auto foo (char t ){
static float g = 42.0f;
}The static in above declarations does different things (yes I know the variables behave kinda same but static does different things to them).
Therefore, I propose to divide the static keyword into 3 parts to address each use case differently.
- The first use case is the only one with radical change. Instead of using keyword
static, we could agree on some different keyword that captures intent clearly in an unambigious way.
//cpp1 code
static int a = 0;
//cpp2 code
SOMETHING a := 0; //not sure on position of keyword I think something like global can be used but put down your suggestions.
-
This use case is already covered by anonymous namespaces in cpp and I think that's how it should be in cpp2. So no change here. Anonymous namespaces are a bit more verbose so if anyone has better idea, do suggest.
-
This use case is partially already covered in cpp2. Static member functions are easily and distinguishably declared in cpp2 without the use of
statickeyword. So I'll be addressing the data member part only. The simple way would be to have a keyword. The real decision to make is whether it would be same as keyword chosen for 1) or a different one. A discussion on this topic would probably be needed so this currently is left unaddressed.
Note: I'd like to expand a bit on 1) to cover global variables too. They also have static lifetime. We can make it mandatory for global variables to be declared with some keyword (same as that would be decided for 1). Herb has plans to make global variables constexpr by default and is not sure if he'll even allow them or not. Having a keyword will capture attention of programmer and also be consistent with other static lifetime variables that would be declared.
These changes will make different things look different and do away with one small source of confusion to new programmers. This is a very small change so shouldn't be hard to implement.
Alternatives
We could also come up with different syntax for case 3) and leave case 1) as it is. That would be one option.
Activity
JohelEGP commentedon Apr 5, 2023
AbhinavK00 commentedon Apr 5, 2023
Ohh, is there a way for static data members too?
JohelEGP commentedon Apr 5, 2023
Seems like that's not the case.
msadeqhe commentedon Apr 5, 2023
@AbhinavK00, so we have the following cases for
statickeyword in C++1:1. Local variables with static life-time
I think this case isn't safe, because the life-time of a local variable will be extended without any obvious tracking.
2. Global variables with internal linkage
Anonymous namespaces are good enough as you have suggested:
or simply:
3. Static function members
Yes, we already have it. This is the example from @JohelEGP:
4. Static data members
Using a keyword for this case seems natural as you have suggested:
Alternatively the following syntax can be used to eliminate
KEYWORDlike other cases don't have any additional keyword:or this one:
In my opinion,
type::variableis better thanX::variable, because the type name doesn't have to be replicated multiple times.AbhinavK00 commentedon Apr 5, 2023
I think this would conflict with how we declare members outside of a class. The
type::variablesyntax would not, but it's close enough to be confusedmsadeqhe commentedon Apr 5, 2023
Would you provide an example? I don't know how it will conflict.
AbhinavK00 commentedon Apr 5, 2023
Ah sorry, my bad. You're right, there would be no conflict.
msadeqhe commentedon Apr 5, 2023
No problem 😉
leejy12 commentedon Apr 8, 2023
sharedcould be a possible candidate, as in this variable is "shared" by all instances of this type.orent commentedon Apr 11, 2023
So which of these uses do you actually refer to as “static” in everyday speech? I know that I refer to 1 and 3 as “static” and 2 as “non exported symbol” or something like that. Use 2 is also the one that has a good alternative that highlights the fact that it is about symbol visibility rather than lifetime.
No new keyword please. The keyword should match the terminology and the terminology should match that of C++ because Cpp2 shares the same underlying object model. Don’t fork the terminology.
AbhinavK00 commentedon May 14, 2023
Closing now as not high priority, may reopen later.
AbhinavK00 commentedon Jun 28, 2023
Another suggestion for use-case 2) would be to make global variables have internal linkage by default. As globals constants and globals constant expressions have internal linkage by default, it would be wise to do the same with global variables and have a keyword to specify external linkage instead.