Matthieu M. Matthieu M. - 12 days ago 7
C++ Question

Deprecation of the static keyword... no more?

In C++ it is possible to use the

static
keyword within a translation unit to affect the visibility of a symbol (either variable or function declaration).

In n3092, this was deprecated:


Annex D.2 [depr.static]

The use of the static keyword is deprecated when declaring objects in namespace scope (see 3.3.6).


In n3225, this has been removed.

The only article I could find is somewhat informal.

It does underline though, that for compatibility with C (and the ability to compile C-programs as C++) the deprecation is annoying. However, compiling a C program directly as C++ can be a frustrating experience already, so I am unsure if it warrants consideration.

Does anyone know why it was changed ?

Answer

In C++ Standard Core Language Defect Reports and Accepted Issues, Revision 94 under 1012. Undeprecating static` they note:

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.

Basically saying that the deprecation of static doesn't really make sense. It won't ever be removed from C++, and it's still useful because you don't need the boilerplate code you need with unnamed namespaces, if you just want to declare a function or object with internal linkage.