Thread: namespace

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    namespace

    We import namespace into scope by "using" keyword. Is it possible to kick it out of scope?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Im not sure, but try looking at the header library files with your compiler. If you can understand what they mean, try impementing a function that might do the opposite. Of course, a compiler has loads of header files with it, but try namespace.h or even winproc.h.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Something like that?

    Code:
    #include <iostream>
    
    namespace John {
        void Message()
        {
            std::cout << "John's message!" << std::endl;
        }
    }
    
    namespace Mary {
        void Message()
        {
            std::cout << "Mary's message!" << std::endl;
        }
    }
    
    using John::Message; //global namespace
    int main()
    {
        
        Message();  //John::
        { 
            using Mary::Message; //in this scope
            Message(); //Mary::
        }
        Message();  //John::
        std::cin.get();
    }
    Actually just typing std:: (or any other namespace identifier) each time where needed quickly becomes a second nature. And it makes code clearer.

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Something like this
    Code:
    using namespace std;
    //code 
    unuse namespace std;
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I don't think it is possible. A namespace is essentially the introduction of a scope. The using directive (using namespace foo) brings the whole of that scope into view. Only by leaving the scope at which the using directive was used will you essentially remove access to it.

    The same happens to the using declaration (using foo:bar). It brings a name into view at the current scope. You will need to leave that scope to remove that name from view.

    Creating nested scopes will not help because, as you probably already know, scopes carry on downwards.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You should just start typing std::cout, std::cin, std::string etc. If you are writing your own header files, you are strongly discouraged to use global using after all. And it's not as inconvenient as it may seem.

    Then you won't have problems that you might have.

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    You should just start typing std::cout, std::cin, std::string etc. If you are writing your own header files, you are strongly discouraged to use global using after all.
    It is exactly what I want to escape from and reason of this thread:-).
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Escaping good practices tends to lead to problems

    If you have some identifiers that are conflicting with things in std namespace, you could put them in a separate namespace and use them with the namespace identifier (e.g my::cout) when they are needed.

    Code:
    #include <iostream>
    using namespace std;
    
    namespace my {
        int cout;
    }
        
    int main()
    {
        
        my::cout = 5;
        cout << my::cout << endl;
        cin.get();
    }
    Or just avoid conflicting names?

  9. #9
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    It is just a technical question. I don't have a real world problem.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I think he wants it so that he can use a namespace in one place (like a header), then unuse it so it doesn't affect whatever program he's writing; where he may be using another namespace.

    I've no idea I'm afraid.

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I need a new keyword: "dontuse"
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Other than introducing scopes into the code there is no other way.

    Code:
    int main() {
    
        int a = 12;
        
        std::cout << a << '\n'; // Ok
        
        {
            using namespace std;
            cout << a << '\n'; // Ok
        }
        
        cout << a << '\n'; \\ Error!
    
    }
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I reckon the only way to unuse is to not use...

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >It is just a technical question. I don't have a real world problem.
    Namespaces aren't like macros. If you use them correctly, the hypothetical problem you're talking about simply doesn't exist.
    My best code is written with the delete key.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Explicit scoping, just do it.
    Code:
    #include <iostream>
    
    namespace Mary {
      void message() { std::cout << "Mary says hello.\n"; }
    }
    
    namespace John {
      void message() { std::cout << "John says hello.\n"; }
    }
    
    namespace Walter {
      void message() { std::cout << "Walter says hello.\n"; }
    }
    
    int main()
    {
      Walter::message();
      John::message();
      Mary::message();
    }
    Lame.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern namespace?
    By Brafil in forum C++ Programming
    Replies: 2
    Last Post: 01-10-2009, 08:06 AM
  2. namespace problem
    By DL1 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2008, 12:10 PM
  3. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM