-
Notifications
You must be signed in to change notification settings - Fork 259
[SUGGESTION] Static: Keyword with many meanings #322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
|
Ohh, is there a way for static data members too? |
Seems like that's not the case. |
@AbhinavK00, so we have the following cases for 1. Local variables with static life-timeI 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 linkageAnonymous namespaces are good enough as you have suggested:
or simply:
3. Static function membersYes, we already have it. This is the example from @JohelEGP:
4. Static data membersUsing a keyword for this case seems natural as you have suggested:
Alternatively the following syntax can be used to eliminate
or this one:
In my opinion, |
I think this would conflict with how we declare members outside of a class. The |
Would you provide an example? I don't know how it will conflict. |
Ah sorry, my bad. You're right, there would be no conflict. |
No problem 😉 |
|
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. |
Closing now as not high priority, may reopen later. |
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. |
In cpp, the keyword
static
is currently used at three different places: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:
The
static
in above declarations does different things (yes I know the variables behave kinda same butstatic
does different things to them).Therefore, I propose to divide the
static
keyword into 3 parts to address each use case differently.static
, we could agree on some different keyword that captures intent clearly in an unambigious way.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
static
keyword. 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.
The text was updated successfully, but these errors were encountered: