Should I avoid using namespace std?

This is a discussion on Should I avoid using namespace std? within the C++ Programming forums, part of the General Programming Boards category; In some cases, you may find that you want to use a using directive because the namespace name is just ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    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();
    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

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    2,899
    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
    682
    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 09:42 AM.

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    2,899
    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

Page 2 of 2 FirstFirst 12
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, 06:14 PM
  2. How to avoid typing std:: (scope resolution operator)
    By Sharan in forum C++ Programming
    Replies: 9
    Last Post: 04-30-2006, 09: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, 05:41 PM
  5. using namespace std, What does it do?
    By Ben K. in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2001, 11:42 PM

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