Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Unnamed namespace over static

Can I assume an object declared in unnamed namespace to be equivalent to as if were static?

namespace { int x; };//  #1

static int x; // #2

FWIK, In both cases, x will have static storage duration and internal linkage.
So does all the rules of an object declared as static applies to an object in unnamed namespace?

For example:

  • What will be the order of construction and destruction? will it be same?
  • Can I use extern keyword with x in unnamed namespace?
like image 489
cpx Avatar asked Nov 23 '11 02:11

cpx


People also ask

Why is an unnamed namespace used instead of static?

Unnamed namespaces are typically used when you have a lot of content that you want to ensure stays local to a given file, as it's easier to cluster such content in an unnamed namespace than individually mark all declarations as static .

What is the purpose of unnamed namespace?

Unnamed Namespaces They are directly usable in the same program and are used for declaring unique identifiers. In unnamed namespaces, name of the namespace in not mentioned in the declaration of namespace. The name of the namespace is uniquely generated by the compiler.

What does anonymous namespace mean?

November 19, 2020. Anonymous namespaces in C++ allow you to define locally-visible-only artifacts in C++. the static keyword provides equivalent (depending on C++ version) locally-visible-only variables and functions. //impl.cpp. //neither can be used externally.

Can a namespace be private?

If you force to create a private class in Namespace, compiler will throw a compile time error "Namespace elements cannot be explicitly declared as private, protected or protected internal" . There are only two valid declarations for a class at the namespace level, "Internal" and "Public".


2 Answers

Most of your questions are answered here. For the rest:

What will be the order of construction and destruction? will it be same?

The order is unchanged from regular globals. So it isn't the same as static.

That being said, I strongly urge you to write code that does not care about the order. The less you rely on the specific order of initialization for any globals, the better.

Can I use extern keyword with x in unnamed namespace?

No. In order to extern something, you have to be able to type its name. And the magic of the unnamed namespace is that you can't type its name. The name is assigned by the compiler. You don't know it. So if you attempt to extern it, you will instead be externing something else.

If you put an unnamed namespace in a header, every translation unit that includes it will get a different version of the variable. They'll all be extern, but they'll be talking about a different external variable.

like image 128
Nicol Bolas Avatar answered Sep 21 '22 17:09

Nicol Bolas


Both have internal linkage (the one in the unnamed namespace only in c++11) but the one in the unnamed namespace is not a member of the global namespace. so for example you can put an x into the unnamed namespace and the global namespace and they will not conflict.

like image 42
Johannes Schaub - litb Avatar answered Sep 19 '22 17:09

Johannes Schaub - litb