Are objects in anonymous namespace implicitly static?

后端 未结 2 1960
独厮守ぢ
独厮守ぢ 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:37

    C++11, 3.5/4:

    An unnamed namespace or a namespace declared directly or indirectly within an unnamed namespace has internal linkage. All other namespaces have external linkage. A name having namespace scope that has not been given internal linkage above has the same linkage as the enclosing namespace if it is the name of — a variable ...

    So in C++11 both of your foo objects have internal linkage. In C++03, the first one has external linkage.

    Regardless of linkage, it has static storage duration.

    I don't think there's any such thing in the standard as "take the object as if it was marked static", so I can't answer to that. If you find some text in the standard that refers to whether the object is "marked static" and doesn't refer specifically to either linkage or storage duration, then cite it and I can give you an opinion :-)

提交回复
热议问题