Like Tree3Likes
  • 3 Post By phantomotap

using namespace std;

This is a discussion on using namespace std; within the C++ Programming forums, part of the General Programming Boards category; my textbook encourages us to use using namespace std; after our libriaries. On this forum I'm noticing that some do ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58

    using namespace std;

    my textbook encourages us to use using namespace std; after our libriaries. On this forum I'm noticing that some do not. I'm trying to understand what is the reason to use it or not use it, other than having to type std all the time. thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    The general rule is that you should not use such a using directive or a using declaration (e.g., using std::cout; ) in a header file except within a restricted scope, or before a header inclusion. Other than that it is a matter of discretion how you want to trade convenience for the risk of a name collision (but there are rare cases where a using declaration is needed, e.g., to unhide names from a base class that may otherwise be hidden). Note that you can still fully qualify names even with a using directive/declaration in effect, and that you can use a namespace alias as an alternative to shorten a long namespace name.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    o.k. thanks!

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,415
    Only to add a little to what has been said, if you need to "bring in" some names from a namespace for the sake of simplicity or resolution at multiple scopes but for whatever reason can't or do not wish to "bring in" all of the names from a given namespace at the relevant scope you can use a two part process thanks to "using declarations" and "using directives" thus simplifying the process without undue repetition or nasty macros.

    Soma

    Code:
    namespace StreamStuff
    {
        using stdp::fstream;
        using stdp::ifstream;
        using stdp::ofstream;
        using stdp::iostream;
        using stdp::istream;
        using stdp::ofstream;
        using stdp::stringstream;
        using stdp::istringstream;
        using stdp::ostringstream;
    }
    
    void BigFunction1()
    {
        using namespace StreamStuff;
        // a lot of standard stream classes are now in scope from our local port
    }
    
    void BigFunction2()
    {
        using namespace StreamStuff;
        // a lot of standard stream classes are now in scope from our local port
    }
    
    void SomeOtherFunction()
    {
        using namespace std;
        // all standard stuff is now in scope from provider libraries
    }
    Salem, xentaka and stahta01 like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Friend of namespace or of namespace function?
    By Programmer_P in forum C++ Programming
    Replies: 18
    Last Post: 03-13-2011, 07:46 PM
  2. need ; before using namespace std
    By wwfarch in forum C++ Programming
    Replies: 7
    Last Post: 05-11-2004, 11:42 PM
  3. using namespace std;
    By Jotun in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2004, 05:00 PM
  4. std namespace
    By bigSteve in forum C++ Programming
    Replies: 8
    Last Post: 01-20-2004, 02:56 AM
  5. namespace help
    By dodge_tomahawk in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2004, 06:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21