tags:

views:

94

answers:

4

I can't find a good explanation about global non static variables in unnamed namespace. I avoid global variables as much as I can. In this particular case I'm interested about behaviour just from pure theoretic side.

Suppose the following code:

In a.h

namespace ai {
  class Widget {
    void DoSomething(int param);
  };
}

In a.cc

namespace {
  int x;
  void Helper() {
  }
}

namespace ai {
  void Widget::DoSomething(int param) {
    x = param;
    Helper();
  }
}
  1. If I would create two instances of the same class Widget, will both instances share the same variable x?

  2. Is above behaviour the same if class instances are on the same thread vs different threads?

  3. What if the variable x would be custom type instead of built-in type?

  4. When variable x will be contructed and when destructed?

  5. Is any relation between sizeof(Widget) and such variables?

  6. What aspects are defined in C++ standard and what not?

Any other considerations, e.g. "need to know" about that topic? Maybe anyone could provide good book reference (e.g. "Effective C++..") to read more?

+2  A: 
  1. Yes, there is just one variable x.
  2. Yes, there is just one variable x.
  3. Yes, there is just one variable x.
  4. On startup & on shutdown.
  5. I'm not following the question.
  6. I believe 1-4 above are in the Standard.

It is exactly the same as if you had defined it as static : Global inside the file, but hidden outside of it.

static int x; 
namespace { 
  void Helper() { 
  } 
}

Unnamed namespaces were added specifically to eliminate that particulay usage of static (which has too many different meanings/usages)

James Curran
I'm right saying that it's essentially the same as I would declare the same variable as static?
kriau
thanks for answer, now it's clear.
kriau
A: 
  1. yes. The namespace has given 'x' a fancy name, but its still a static object at global scope.

  2. maybe. threads are not part of the iso c++ virtual machine described in the standards document. how c++ interacts with threads is an implementation defined issue. Most compilers have some sort of declspec directive to make variables thread local otherwise all threads share the same instance.

  3. No difference.

  4. x is an int. its value is undefined / uninitialzied. if it was a class it would be constructed before main() and destructed after.

5 & 6. don't understand the question.

I have one go-to resource for c++ questions: the c++ faq lite

Chris Becke
A: 

sorry, i replied OT.

Stephane Rolland
+3  A: 

If I would create two instances of the same class Widget, will both instances share the same variable x?

Yep. It's a global variable. There can always ever be one - per process, that is.

Is above behaviour the same if class instances are on the same thread vs different threads?

Global variables are global per process (not per thread).

What if the variable x would be custom type instead of built-in type?

Doesn't matter. (Thankfully, C++ sports little differences between built-ins and UDTs.)

When variable x will be contructed and when destructed?

Constructed before main() is called, destructed afterwards. The order of construction of globals across multiple translation units is undefined. Within the same TU it's the order of definition. (Or definition? I forgot.) The order of destruction is well defined and is, as always, the revers construction order.

Is any relation between sizeof(Widget) and such variables?

Why would there be a relation between a class X and a variable Y of a different type?

What aspects are defined in C++ standard and what not?

Basically, a global variable in an unnamed namespace is a normal global variable. Only that it resides in a namespace with a "funny" name that you cannot spell out and which is unique to its translation unit.

sbi
@sbi: many people seems to think that there should be a relation between the size of an object the number of methods / static attributes it has/use. I sometimes wonder if it's the influence of dynamic languages which introduced the confusion for methods and may have spilled over with time.
Matthieu M.
@Matthieu: But even given that, how would the size of class `Widget` relate to a global `int` variable?
sbi
@sbi: I don't know, it's like people expect that a pointer or something be stored at object level, because surely if it uses it and I don't pass it as argument, it needs to have a reference to it... isn't it ?
Matthieu M.
@Matthieu: the question about size supposed to be actual only in the case if answer would be that global non static variable in unnamed namespace is somehow per class instance not per process. I didn't know the answer at the question's ask time. So, now I agree that part about sizeof is not relevant to the question. What is a bit confused that for global variables the static keyword doesn't make any sense.
kriau
@kriau: What `static` does (for global variables, anyhow, `static` has many meanings), is to modify the linkage of a variable, so that it is only visible within its translation unit ("internal linkage"). This used to be done to prevent name clashes with other glibal variables. However, `static` only provides this for _variables_ and _functions_, not for types. This is where unnamed namespaces come in: they provide a uniform way to prevent name clashes for _any_ identifier.
sbi
Also see [this question](http://stackoverflow.com/questions/3525576/internal-linkage-with-static-keyword-in-c/) regarding internal linkage which just came up.
sbi