Static keyword is back!

According to the standard defects report, using the static keyword to declare internally linked entities has been undeprecated. If you did not know, use of the static keyword in this manner was declared deprecated in Annex D.2 in lieu of using unnamed namespaces (

In case you’re confused…

// somefile.cpp

// deprecated version
static int x = 42;

// "better" version
namespace { int x = 42; }

Of course, this deprecation was apparently voted out clear back in November and I’m only now becoming aware of it, but all I’ve got to say is, “About fucking time!” Every time someone told me that I should use anonymous namespaces instead of static variables I’ve had to hold back the urge to yell, “WTF is your malfunction??” The two constructs do totally different things and have totally different purposes. The only time and reason in which anonymous namespaces where better was when you needed a “private” entity that you could use in templates (since they require external linkage). Beyond that, they just gave you more to type and provided external linkage to things you don’t WANT to be externally linked.

Pardon me, but I feel a slight vindicated right now :p

Of course, I’ve never run into a case where it really mattered all that much (except wrt to the template thing) so I capitulated and started using anonymous namespaces instead of static, internally linked variables. It wasn’t like any vendor was going to actually remove the keyword, but in an effort to be “more standard” and use the supposedly “better way” I did it anyway. Now that even the standard committee has decided that this was a dumb move though, I might just go back.


3 Responses to “Static keyword is back!”

  1. Marcelo Cantos Says:

    AFAICT, the un-deprecation has nothing to do with a change of heart, but merely an acknowledgement that there is practically no chance of ever actually outlawing the traditional usage. FWIW, I think that an unnamed namespace is cleaner than static; I usually declare a single “namespace { … }” section at the top of any source file that has file-local stuff, which makes it easy to spot such things (and requires less typing, overall, not more). I also find that this section tends to end up with more than just variables and functions. E.g., file-local classes — which occasionally have name clashes with similar types in other source files — cannot simply be declared static in a conforming program. Perhaps I should worry more about the linkage implications, but, for better or worse, I don’t.

  2. A Static Variable is visible or accessible only within the class or only by members of class But its lifetime is throughout the program means it can hold the same value throughout the lifetime of program

    Need of Static Data Member and Data Functions

    • That’s not what I was talking about and it is false. The static keyword used in such a manner has nothing to do with accessibility of the member, which is governed by the usual accessibility rules.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: