Possible Duplicate:
Why are unnamed namespaces used and what are their benefits?
Looking at someones code and this is what they have declared:
namespace {
struct myStruct {
int x;
int y;
} obj1;
}
..in a function I see it used like this:
myStruct& var = obj1;
(Notice namespace
is anonymous.)
From how I see it used, I can not figure out why it is declared and used like this.
What does declaring it like this do differently?
Also, why is the pointer created like this rather than the traditional style shown here. i.e. myStruct *ptr;
Thank You!
It's a reference, not a pointer, that is created with
myStruct& var = obj1;
The same rationale applies to pointers, however. A lot of C++ programmers prefermyStruct* ptr
overmyStruct *ptr
,myStruct& ref
overmyStruct &ref
. The compiler doesn't care which you use. This style preference is for the reader of the code.The reason for putting the asterisk or ampersand with the type rather than the variable is because that asterisk or ampersand is logically a part of the type. The type of
ptr
is pointer tomyStruct
. A potential problem arises with this scheme:Type* ptr1, ptr2;
Because of rules inherited from C,ptr2
isn't a pointer. It's just anint
.There's an easy solution to this problem. Don't do that! In general, it is better to declare one variable per declaration, with an exception for simple things like
int i,j,k;
Don't mix pointers and non-pointers.It does basically the same thing as the
static
keyword but doesn't actually force internal linkage. The variable is still externally linked, you just have no way of resolving its name in any other translation unit. This was necessary because template arguments must have external linkage, or at least used to require such.var
is also not a pointer, it is a reference. It's not created like other pointers because it is not one.In C++ you are only allowed to have one definition in a given named scope. If there are multiple translation units, you are still only allowed to have one definition but the compiler won't guarantee that all definitions are, indeed, identical. That is, if you need a local type, e.g., a
struct
or aclass
you need to make sure that the definition does not conflict with another type anywhere in any other translation unit. Doing this is pretty much impossible in a large project unless you have a way to somehow locally protect your type. This is what an unnamed namespace provides: any name defined within an unnamed namespace is unique within the entire executable.Everything that's declared inside an anonymous namespace gets a unique, unknowable name, and thus cannot be referred to from any other translation unit. The anonymous namespace is thus guaranteed to be local to the current translation unit only, and never to clash with a different one.
For example, if you say
namespace { int i; }
, you are guaranteed that only the current translation unit sees the globali
. Even if this declaration is in a header that's included in multiple, different TUs, each TU receives its own copy of the global variable (each with a different, unknowable fully-qualified name).The effect was similar to declaring a global object
static
(which gives the global object internal linkage) in C++03, where objects in the anonymous namespace may still have external linkage. In C++11, names in an unnamed namespace have internal linkage as per 3.5/4, so the effect is exactly the same for variables and functions as declaring themstatic
– but internal linkage applies to more than just variables and functions (e.g. enums, classes, templates), so as of C++11, you should always prefer unnamed namespaces!