Does the Global Namespace consist of all translation units that make up a program, or does each translation unit have a Global Namespace of its own?
Printable View
Does the Global Namespace consist of all translation units that make up a program, or does each translation unit have a Global Namespace of its own?
Neither.
Any translation unit (which, in C++, usually means "source file") can place symbols into any namespace. There are some restrictions if the namespace is associated with a class scope. It is also necessary to obey the "One Definition Rule" (meaning that two translation units cannot define the same symbol in a given namespace).
Local variables within a function are not placed within any namespace.
Global variables (i.e. at file scope, outside any function) which are local to one translation unit (eg not declared "extern") are not placed in any namespace.
Okay I wasn't really understanding what I wasn't understanding (lol) about namespaces for a minute, but I think I understand it now.
What I wasn't getting was how you could place a function definition in a namespace and use the namespace in multiple files, but at that time I was thinking that the namespace was more like a header file (which it isn't a good idea to put function definitions in).
Now I understand that namespaces simply deal with the names that are available, is this correct?
Putting a using directive in multiple files for a namespace containing a function won't cause the function to be defined twice, because the function is only defined in the namespace and the directive only makes the function name available in the files, correct?
Another thing I think I was trying to get at by asking the question in the first post was, putting a using directive at file scope only makes the names available for that file, correct?
Namespaces are essentially a grouping of names. The namespace name coupled with the name of whatever you're working with is unambiguous. If you don't specify the namespace, it can be ambiguous.Quote:
Originally Posted by homeyg
All a using directive does is tell the compiler that the particular names it specifies are CANDIDATES when it sees a particular name.Quote:
Originally Posted by homeyg
A "using namespace" directive simply tells the compiler that everything in the specified namespace is a candidate for matching a name that is not adorned with the name of the namespace. If a name is encountered that is in two such namespaces, there are two candidates to match it, and the compiler complains about ambiguity.
More specific "using" directives affect particular names (for example "using std::string;" means that std::string is a candidate for matching a name of something that is simply "string")
For example;
Code:#include <string> // brings in std::string and associated things
namespace X
{
typedef char string[40];
}
using namespace X;
using namespace std;
void func()
{
string x; // ambiguous, as both X::string and std::strings are candidates
std::string y; // OK; std::string is unambiguous
X::string z; // OK; X::string is unambiguous
}
For source (eg .cpp) files that is true. The compiler does not make leaps of judgement across multiple source files (it does what is referred to as "separate compilation of source files").Quote:
Originally Posted by homeyg
What you say is not true of header files (or any files that are #include'd). Place a using namespace directive into a header file almost always ensures that every source file which #include's that header will be affected by the using directive. One reason that using directives at file scope are discouraged in header files is that there is no way to turn off a "using" directive, meaning that a header file which does it can make source code ambiguous, along the lines of my example above.
Thank you.