3

From the following stackoverflow answer, the user says:

It means that the variable is local to a translation unit (simply put, to a single source file), and cannot be accessed from outside it. This use of static is in fact deprecated in the current C++ Standard - instead you are supposed to use anonymous namespaces:

static int x = 0;

should be:

namespace {
    int x = 0;    
}

I don't disagree that anonymous namespaces are the preferred method,
but is using static really deprecated now?
Where does the standard say this?

Community
  • 1
  • 1
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • I was under the impression that it was `deprecated` but then someone pointed out to me on this site that it was not. – Anon Mail Dec 07 '15 at 17:49
  • Here's a good link: http://stackoverflow.com/questions/4726570/deprecation-of-the-static-keyword-no-more – Anon Mail Dec 07 '15 at 17:50
  • 1
    No, it is not deprecated. But some people want it to be for the reason unclear to me :) – SergeyA Dec 07 '15 at 17:54
  • 1
    The C++ committee seems to be very careful with what they depreciate, so I'd be surprised if they broke the behavior of static this way. – Trevor Hickey Dec 07 '15 at 18:11
  • 1
    @TrevorHickey - deprecating `static` at global scope would not break its behavior in any way, except with overly aggressive settings wrt warnings. Code that is correct now would continue to be correct. – Pete Becker Dec 07 '15 at 18:15
  • @PeteBecker So from what I can tell on Anon Mail's link, is that using static here IS "depreciated", but that does not mean "removal of its functionality". It simply means "no longer encouraged"? – Trevor Hickey Dec 07 '15 at 18:23
  • 1
    When something is deprecated it means that it might be removed in a later standard. Nothing more. (it has some procedural implications for the standards committee, but that's not of concern here) – Pete Becker Dec 07 '15 at 18:28
  • 1
    IIRC, that use case was deprecated in C++11 and then undeprecated later once everyone realized it was pointless, since it could never be removed, given C compatibility concerns and all that. – Praetorian Dec 07 '15 at 18:29
  • 1
    @TrevorHickey: Yep that's what deprecated means: Its still valid (but may in the future be removed). I heard that this is what they tried to do. But I was under the impression this never made it into the final version. But I have no proof (this is just something I remember hearing). Would love if somebody had a definitive answer. – Martin York Dec 07 '15 at 18:29

1 Answers1

2

No, it is not currently deprecated. It was at one point but this was reversed due to C comparability issues. At some point before 1999 it was deprecated and this lead to defect report 174 which says:

The decision to deprecate global static should be reversed.

  • We cannot deprecate static because it is an important part of C and to abandon it would make C++ unnecessarily incompatible with C.
  • Because templates may be instantiated on members of unnamed namespaces, some compilation systems may place such symbols in the global linker space, which could place a significant burden on the linker. Without static, programmers have no mechanism to avoid the burden.

This lead to defect report 223 in which the meaning of deprecation was revised from:

deprecated is defined as: Normative for the current edition of the Standard, but not guaranteed to be part of the Standard in future revisions.

it was noted that this implies, only non-deprecated features will be support in future standards:

However, this definition would appear to say that any non-deprecated feature is "guaranteed to be part of the Standard in future revisions." It's not clear that that implication was intended, so this definition may need to be amended.

and changed the meaning of deprecated to:

These are deprecated features, where deprecated is defined as: Normative for the current edition of the Standard, but having been identified as a candidate for removal from future revisions.

and later the feature was undeprecated due to C compatibility issues by defect report 1012:

Although 7.3.1.1 [namespace.unnamed] states that the use of the static keyword for declaring variables in namespace scope is deprecated because the unnamed namespace provides a superior alternative, it is unlikely that the feature will be removed at any point in the foreseeable future, especially in light of C compatibility concerns. The Committee should consider removing the deprecation.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740