CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: What is the Global Namespace

  1. #1
    Join Date
    Aug 2002
    Location
    ireland
    Posts
    291

    What is the Global Namespace

    What is the Global namespace?

  2. #2
    Join Date
    May 2005
    Posts
    99

    Re: What is the Global Namespace

    Code:
    Program #1
    
    #include <iostream>
    #include <alphabets.h>
    
    letter alps {'a','b',....}; //alps goes in global namespace
    So your variable is now accessible globally without creating any object or using functions. It is global for all those who use the header file of above code.

    One more e.g. #include <iostream> is a header file provided by compiler. So it contains all functions in namespace std. Since std is global, including iostream allows us to access all methods withut :: operator.
    If you use gcc v3 and above you will find that std is no more global. You have to write std::cin for cin.

  3. #3
    Join Date
    Aug 2002
    Location
    ireland
    Posts
    291

    Re: What is the Global Namespace

    Thanks,
    So I have come across topics recommending not using the global namespace and global namespace pollution, is this because of the possibility of naming conflicts or is there some other reason? Surely the compiler would pick these conflicts up!

    Can you create objects on the heap from the global namespace, and it so where are they/should they be deleted?

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb Re: What is the Global Namespace

    Quote Originally Posted by ireland
    What is the Global namespace?
    did u checked MSDN

    The C++ language provides a single global namespace. This can cause
    problems with global name clashes. For instance, consider these two C++
    header files:

    char func(char);
    class String { ... };

    // somelib.h
    class String { ... };
    With these definitions, it is impossible to use both header files in a single
    program; the String classes will clash.

    A namespace is a declarative region that attaches an additional identifier to
    any names declared inside it. The additional identifier makes it less likely
    that a name will conflict with names declared elsewhere in the program. It
    is possible to use the same name in separate namespaces without conflict
    even if the names appear in the same translation unit. As long as they
    appear in separate namespaces, each name will be unique because of the
    addition of the namespace identifier. For example:

    Code:
    namespace one
    {
        char func(char);
        class String { ... };
    }
    
    // somelib.h
    namespace SomeLib
    {
        class String { ... };
    }

    Now the class names will not clash because they become one::String and
    SomeLib::String, respectively.


    Declarations in the file scope of a translation unit, outside all namespaces,
    are still members of the global namespace

  5. #5
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    Re: What is the Global Namespace

    Quote Originally Posted by ireland
    Thanks,
    So I have come across topics recommending not using the global namespace and global namespace pollution, is this because of the possibility of naming conflicts or is there some other reason? Surely the compiler would pick these conflicts up!

    Can you create objects on the heap from the global namespace, and it so where are they/should they be deleted?
    The global namespace is "the area" surrounding any namespaces declared explicitely, regardless whether these are named or not. The purpose of namespaces is to group together things that logically belong together. Because of this, if you bloat a namespace into the global namesace (a.k.a "pollute the global namespace") you distroy this logical grouping. As a side effect, the compiler won't be able any more to distinguish between constructs that have the same name but belonged to different namespaces. In most cases the compiler will pick the conflict up and generate an error but there are cases where the compiler will be able to solve the conflict on its own in a way unintended by the programmer. This leads to hard to track bugs. Example:
    Code:
    namespace my_namespace
    {
        void f(int i)
        {
            // do something
        }
    }
    
    void f(float x)
    {
        // do something else);
    }
    //version 1:
    using namespace my_namespace;
    // version 2: comment out the above line
    
    
    
    int main()
    {
        f(1); // version 1: will call my_namespace::f(1), because it's the better match
              // version 2: will call the f(1) in the global namespace
        return 0;
    }
    Creating objects (on the free store -- a.k.a. heap -- or otherwise) has nothing to do with namespaces. If you say
    Code:
    A* a = new A();
    the compiler will search for the type "A" beginning with the innermost namespace (the one where your line of code lives in) up to the global namespace and pick the first A that qualifies. The heap itself doesn't know about namespaces at all.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  6. #6
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: What is the Global Namespace

    Quote Originally Posted by manish_velankani
    One more e.g. #include <iostream> is a header file provided by compiler. So it contains all functions in namespace std. Since std is global, including iostream allows us to access all methods withut :: operator.
    If you use gcc v3 and above you will find that std is no more global. You have to write std::cin for cin.
    You can make std appear global by putting

    Code:
    using namespace std;
    into your code. Then you will no longer need to use std:: but this is bad practice to do on a global level. It is reasonable on a local level.

    Local level would mean:
    - in a compilation unit (.cpp file) rather than a header
    - in a header but within another namespace. Then std is only included within your namespace
    - in a header file but within a class definition. Then std is only included within that class.

  7. #7
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: What is the Global Namespace

    You may like to visit this tutorial - Namespaces. I have attached an image that might help you understand the global namespace better. You could consider the global namespace as a superset of all the namespace and all code would be inside this global namespace (code inside individual namespace are contained in those namespaces respectively but that is again there inside the global one).
    Attached Images Attached Images  
    Last edited by exterminator; August 9th, 2005 at 07:51 AM.

  8. #8
    Join Date
    Jul 2005
    Posts
    103

    Re: What is the Global Namespace

    Quote Originally Posted by NMTop40
    - in a header file but within a class definition. Then std is only included within that class.
    Last point is incorrect as this is actually disallowed.
    Code:
    #include <vector>
    namespace NS {}
    struct A { void foo(); };
    class B : A{
      using A::foo;        // ok
      using std::vector;   // error, using declaration disallowed, non-member
      using namespace std; // error, using directive disallowed at class scope
      using namespace NS; // error, using directive disallowed at class scope
    };

  9. #9
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: What is the Global Namespace

    Wasn't sure. I've never tried doing it.

  10. #10
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: What is the Global Namespace

    Quote Originally Posted by freddyflintstone
    Last point is incorrect as this is actually disallowed.
    Code:
    #include <vector>
    namespace NS {}
    struct A { void foo(); };
    class B : A{
      using A::foo;        // ok
      using std::vector;   // error, using declaration disallowed, non-member
      using namespace std; // error, using directive disallowed at class scope
      using namespace NS; // error, using directive disallowed at class scope
    };
    I've come across this problem of not being able to use a using declaration inside a class and I came up with a very neat solution:

    Code:
    namespace mediator {
    
    using std::vector;
    
    class myclass
    {
        ...
    };  // class myclass
    
    }  // namespace mediator
    
    using mediator::myclass;
    This has the same effect as if you would put the usind std::vector in the class definition.
    Old Unix programmers never die, they just mv to /dev/null

  11. #11
    Join Date
    Jul 2005
    Posts
    103

    Re: What is the Global Namespace

    Quote Originally Posted by HighCommander4
    I've come across this problem of not being able to use a using declaration inside a class and I came up with a very neat solution:

    Code:
    namespace mediator {
    
    using std::vector;
    
    class myclass
    {
        ...
    };  // class myclass
    
    }  // namespace mediator
    
    using mediator::myclass;
    This has the same effect as if you would put the usind std::vector in the class definition.
    Which is more or less the point made earlier.
    Quote Originally Posted by NMTop40
    - in a header but within another namespace. Then std is only included within your namespace

  12. #12
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: What is the Global Namespace

    Is

    Code:
    using std::vector;
    allowed anywhere? It's a template and I don't think it is.

    Within a class the easiest way to "use" something from a namespace is to typedef it thus:

    Code:
    class MyClass
    {
       typedef std::string string;
    };

  13. #13
    Join Date
    Jul 2005
    Posts
    103

    Re: What is the Global Namespace

    Quote Originally Posted by NMTop40
    Is

    Code:
    using std::vector;
    allowed anywhere? It's a template and I don't think it is.
    Its allowed in namespace (global, named and unnamed namespace) and function scope.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)