Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are objects in anonymous namespace implicitly static?

Tags:

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; } 

and

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

?


From questions and answers around here I was believing that objects in anonymous namespace were implicitly static, but someone tell me it's the linkage only that's internal, the compiler will not take the object as if it was marked static with implications like how it will implement object construction. So I need some details about what it really means, if there is a difference between with and without static in anonymous namespace.

like image 621
Klaim Avatar asked Nov 15 '12 11:11

Klaim


People also ask

Are variables in anonymous namespace static?

Theoretically, extern variables declared in anonymous namespace is a superior alternative over a simple static variable. That's why the "static global variables" were deprecated before C++11.

Is a static definition in anonymous namespace static is redundant here?

readability-static-definition-in-anonymous-namespace Finds static function and variable definitions in anonymous namespace. In this case, static is redundant, because anonymous namespace limits the visibility of definitions to a single translation unit.

Why is an unnamed namespace used instead of static?

1.1 Unnamed namespaces, paragraph 2: The use of the static keyword is deprecated when declaring objects in a namespace scope, the unnamed-namespace provides a superior alternative. Static only applies to names of objects, functions, and anonymous unions, not to type declarations.

What does an anonymous namespace do?

2)We can have anonymous namespaces (namespace with no name). They are directly usable in the same program and are used for declaring unique identifiers. It also avoids making global static variable. The “anonymous” namespace you have created will only be accessible within the file you created it in.


1 Answers

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 :-)

like image 182
Steve Jessop Avatar answered Nov 13 '22 14:11

Steve Jessop