8

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?
2
  • The specific questions at the end are not covered. Commented Nov 23, 2011 at 3:19
  • @aschepler: true, I've just looked into other question. voting to reopen this. Commented Nov 23, 2011 at 3:32

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Could you expand on the difference between a global (externalizable) variable and a file-static variable when it comes to the order of construction and destruction ? I thought (perhaps naively) that it was the same.
@MatthieuM.: If I recall correctly, file-static variables are all initialized before globals. I could be wrong though. Which is yet another reason to not rely on it.
@NicolBolas: I think you are confusing file-static with the zeroing of memory. Indeed initialization of globals happen in two phases. In a first phase, built-ins (and structs with no constructors) are initialized from constants and the rest (which require function execution) is zeroed; in a second phase, the functions are executed (including constructors). This is what guarantees that you can test a global pointer for nullity and set it on first (real) use.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.