Thread: Should I avoid using namespace std?

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    In some cases, you may find that you want to use a using directive because the namespace name is just too long. In such cases, you should consider a namespace alias instead, e.g.,
    Code:
    namespace jimblumberg
    {
        void foo()
        {
            // ...
        }
    }
    
    // ...
    
    namespace jbb = jimblumberg;
    
    jbb::foo();
    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

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Or you may have some nested namespaces that are too long. Then you can also use an alias to shorten it up.

    Code:
    namespace jimblumberg
    {
       void foo();
       {
       }
    
       namespace laserlight
       {
          void bar()
          {
          }
       }
    }
    
    
    namespace jimblumberg::laserlight = jbll;
    
    
    jbll::bar();

    Jim

  3. #18
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    I always make my namespaces up to 6 characters long (the global one is usually the same as the target binary, e.g. for target 'prog32.exe' I use namespace 'prog' etc.) so there is no need to prefix names with long scope names.

    And maybe someone has already mentioned about it:

    If you place a 'using' declaration with a namespace you will be able to access all of the namespaces that are used within the specified namespace.

    If 'first' is using 'std' and 'second' is using 'first', then 'second' is using 'std' too.
    Last edited by kmdv; 10-22-2010 at 08:42 AM.

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I prefer to fully qualify my namespaces.

    Code:
    std::cout << "Hello World" << std::endl;
    I have had problems with the using directive in the past, the same function defined in the global namespace and in namespace std. It took me several hours to find the problem, and thanks to forum members I finally was able to fix the problem.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thoughts about using namespace std?
    By dwks in forum C++ Programming
    Replies: 40
    Last Post: 09-25-2008, 05:14 PM
  2. How to avoid typing std:: (scope resolution operator)
    By Sharan in forum C++ Programming
    Replies: 9
    Last Post: 04-30-2006, 08:25 PM
  3. using namespace std;
    By spank in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2006, 06:28 AM
  4. *sigh* Using Namespace Std; Woes
    By KneeLess in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2003, 04:41 PM
  5. using namespace std, What does it do?
    By Ben K. in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2001, 10:42 PM