Are objects in anonymous namespace implicitly static?

后端 未结 2 1961
独厮守ぢ
独厮守ぢ 2021-02-12 09:04

From the C++11 standard point of view, is there a technical difference of object status/properties between:

namespace
{
   int foo;
   const int bar = 42;
}
         


        
2条回答
  •  伪装坚强ぢ
    2021-02-12 09:33

    There is a very big difference: with static, the name has internal linkage; without it, it has external linkage (but is in a namespace you can't cite in another translation unit). The difference isn't always important, but it does come into play in a few cases: if you have a template with an int* parameter, for example, you cannot instantiate it on &foo if foo is declared static.

    EDIT:

    As Steve Jessop has pointed out, this is only true in C++03. C++11 does make a change here. Although I don't know why; I find the C++03 specification more coherent—the namespace something is in shouldn't affect linkage.

提交回复
热议问题