6

I was recently reading Stroustrups The C++ Programming Language and in the section about Linkage in chapter 9 I came across the following paragraph:

"In C and older C++ programs, the keyword static is (confusingly) used to mean "use internal linkage". Don't use static except inside functions and classes."

The problem is that reading further on, the author did not elaborate on why this is bad practice. I use static functions from time to time in my code usually for some simple calculation that is not needed outside the compilation unit, but I never realised this was frowned upon and it is not obvious to me why it is bad. Can anyone shed light on this for me please??

ZnArK
  • 1,533
  • 1
  • 12
  • 23
mathematician1975
  • 21,161
  • 6
  • 59
  • 101

2 Answers2

5

I think that the idea is not to frown upon internal linkage, but to avoid confusion with the new meaning of static: static has way too many meanings (declaring stuff with internal linkage, defining local variables with static storage duration, marking non-instance members of classes), so avoiding one of the less intuitive ones is a good thing.

Because of this, the C++98 standard provides another way to declare stuff with internal linkage: unnamed namespaces, and deprecates static "when declaring objects in a namespace scope".

The C++11 standard removed this deprecation (and also changed somehow the semantic of unnamed namespaces, not really requiring internal linkage), so it's now really a matter of style.

Community
  • 1
  • 1
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • 3
    Unnamed namespaces do _not_ make their contents have internal linkage. They place it in a namespace which can't be named outside of the translation unit, but the linkage is still external, which makes a difference for templates. – James Kanze Jul 02 '12 at 17:28
  • @JamesKanze: Does it still make a difference for templates, or are you referring to the pre-2011 language? – Mike Seymour Jul 02 '12 at 17:56
  • @JamesKanze: what difference does it make for templates? – Matteo Italia Jul 02 '12 at 19:53
4

Because in C++ anonymous namespaces should be preferred, which basically offer the same functionality.

I think static in this context is frowned upon because of the double meanings, and the fact that the two meanings are quite opposite - if inside a class or a struct it represents a global state, whereas outside it gives internal linkeage and provides a copy of a variable or method to each translation unit.

Moreso, static can only apply to functions and variables, whereas inside an anonymous namespace you can have type declarations.

namespace  //OK
{
    class X {};
}

static class X //syntactically correct, but does nothing
{
};

Note that the standard deems the use of static to specify linkeage as deprecated.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • There's also the fact that you cannot use `static` objects or functions to instantiate a template. (And there's the somewhat contradictory fact that a `const` variable at namespace scope is `static` by default.) – James Kanze Jul 02 '12 at 17:25
  • 1
    C++11 removed the deprecation, and also the silly restriction on template arguments. There's no longer any particular reason to prefer one over the other. – Mike Seymour Jul 02 '12 at 17:33