Thread: Quick question about namespaces

  1. #1
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209

    Quick question about namespaces

    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?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.

  3. #3
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    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?
    Last edited by homeyg; 03-05-2006 at 08:57 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by homeyg
    Now I understand that namespaces simply deal with the names that are available, is this 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
    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?
    All a using directive does is tell the compiler that the particular names it specifies are CANDIDATES when it sees a particular name.

    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
    }
    Quote Originally Posted by homeyg
    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?
    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").

    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.

  5. #5
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM