Thread: using namespace std;

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    5,108
    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
    }

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, 06:46 PM
  2. need ; before using namespace std
    By wwfarch in forum C++ Programming
    Replies: 7
    Last Post: 05-11-2004, 10:42 PM
  3. using namespace std;
    By Jotun in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2004, 04: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