1

Consider:

namespace JohnsLib {
    static bool foobar();
    bool bar();
}

What implications does static have here?

John
  • 6,433
  • 7
  • 47
  • 82
  • I'm assuming it means you can call foobar() from any place that is using the JohnsLib namespace. But that foobar would be static. – Nic Foster Jan 25 '12 at 18:14

4 Answers4

5

It changes the linkage from "external" to "static", making it invisible to the linker, and unusable from other compilation units. (Well, if other compilation units also include the header, they get their own separate copy)

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • That is very interesting, I'm sure someone will take advantage of that, are you sayting that static variables within it would not be shared between compilations units? If so then it the only effect I see is in the way the binary is created, and of course its size. – John Jan 25 '12 at 18:19
  • @John: It might also affect you if you took the address of the function, and then compared it. – Ben Voigt Jan 25 '12 at 19:48
  • I still can't get my head around the practical benefits. Surely that makes for repetition and redundancy? – John Jan 25 '12 at 22:14
  • @John: For functions, the only real benefit is that it enforces "this is an implementation detail, not used by any other code". – Ben Voigt Jan 25 '12 at 23:22
  • Thanks Ben, now I understand. It's a bit like PIMPL without the hassle and convolution. – John Jan 26 '12 at 01:27
  • @John: Somewhat. It might slightly speed link times, because it makes the symbol table smaller. But I look more at the refactoring aspect. If I'm thinking of rewriting a static function (adding an argument, or such) I can be sure that I only affect code in the same file compilation unit. That's a big win from a designer headache standpoint. – Ben Voigt Jan 26 '12 at 01:36
3

static at namespace scope means that it is local to a translation unit (i.e. source file). If you define the function in the header file and include this header into multiple C++ files, you won't get redefinition errors because all the functions will be unique(more correctly, the functions will have internal linkage). The same effect can be achieved by means of anonymous namespaces, for example

namespace JohnsLib
{
    namespace
    {  
         bool foobar() {definition here, won't cause redefinition errors}
    }
    bool bar();
}
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • 2
    But note that `inline` is usually a better way to prevent multiple definition errors on functions defined in headers. – Ben Voigt Jan 25 '12 at 18:16
  • 1
    @Ben: How and why is inline better? – Armen Tsirunyan Jan 25 '12 at 18:31
  • 1
    @ArmenTsirunyan: `inline` declares that all definitions are of the same function; so you'll only end up with at most one copy of the function and any static variables inside it. Internal linkage, or an unnamed namespace, will give multiple separate functions, each with its own static variables. – Mike Seymour Jan 25 '12 at 18:43
  • @Mike I don't think that's right. Inline functions have "external linkage, but the linker is guaranteed not to reject multiple copies. Other than that, they behave *exactly* like regular functions. In particular, the address of an inline function is guaranteed to be unique throughout a program." - Sutter/Alexandrescu C++ Coding Standards #61. The anonymous namespace abuse shown above by Armen is specifically recommended against. – Tod Jan 26 '12 at 02:00
  • @Tod: Yes, that's exactly what I said. – Mike Seymour Jan 26 '12 at 11:45
1

The result of static keyword in namespace scope (global or user defined namespace) is that such define object will not have external linkage; that is, it will not be available from other translation units and cannot be used as a (non-type one i.e. reference or pointer) template parameter.

bronekk
  • 2,051
  • 1
  • 16
  • 18
0

In the C++ programming Language Bjarne states In C and C++ programs,

the keyword static is (confusingly) used to mean "use internal linkage". Don't use static except inside functions and classes.

In Sutter/Alexandrescu C++ Coding Standards Item 61 is "Don't define entities with linkage in a header file."

Tod
  • 8,192
  • 5
  • 52
  • 93