Thread: Why shouldn't "using namespace std" be used?

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    23

    Why shouldn't "using namespace std" be used?

    Is there any specific reason why "using namespace std" must not be used?

    Does it cause interference with other functions for example? I only know that std is used for cout, cin and endl. So using namespace std must only affect these functions right? (Also what else needs to be specified as std::?)

    -> I'm asking because if I were to convert code from Quincy (a much older compiler that doesn't have namespace), I would have to change each and every cout which could be tedious.

    So if it has no short-term disadvantages then I would not bother editing the code from Quincy to suit Visual C++ I would instead just write "using namespace std;"

    Cheers.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,423
    Using "using namespace std" in a header file will result in weird and hard to find problems in code that people will curse you for in the future.

    If it is a .cpp file I would just add the "using namespace std".

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    39
    You can also usually place all your code within an anonymous namespace inside of a header file and then declare "using namespace std" from there. That way you can write your header code without namespace resolution without affecting the users of your library.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,910
    Quote Originally Posted by Sir Galahad
    You can also usually place all your code within an anonymous namespace inside of a header file and then declare "using namespace std" from there.
    I don't think that's wise: the unnamed namespace makes any names declared within it have internal linkage, but typically in header files you intend the names declared to have external linkage. What could be done is to use the using directive in a local scope, typically in the scope of an inline function defined in the header. Another possibility is to have the using directive within a named namespace instead, and one would probably have one's own namespace for one's own header, but that's not as foolproof as an unnamed namespace since a using directive for that namespace would then bring the using directive for the std namespace (or whatever other namespaces were involved in using directives in the custom named namespace) into scope.
    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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,559
    > I would have to change each and every cout which could be tedious.
    That's why editors have global search and replace features


    Rather than dragging in the whole of the std namespace, you have some options.

    Bite the bullet and do a global search/replace.
    Code:
    #include <iostream>
    #include <iomanip>
    int main ( ) {
        int var;
        std::cout << "Type some int" << std::endl;
        std::cin >> var;
        std::cout << "You typed " << var << std::endl;
    }
    Only mention the parts of the std namespace you're interested in (or use the most)
    Code:
    #include <iostream>
    #include <iomanip>
    using std::cout;  // we do this a lot.
    int main ( ) {
        int var;
        cout << "Type some int" << std::endl;
        std::cin >> var;
        cout << "You typed " << var << std::endl;
    }

    When you start writing much larger programs, you'll come across libraries which have their own namespaces as well (random example).

    In short, "using namespace std;" will get you out of the immediate porting hole you're in, but you shouldn't be using it as a way of being lazy in new code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    39
    Quote Originally Posted by laserlight View Post
    I don't think that's wise: the unnamed namespace makes any names declared within it have internal linkage, but typically in header files you intend the names declared to have external linkage. What could be done is to use the using directive in a local scope, typically in the scope of an inline function defined in the header. Another possibility is to have the using directive within a named namespace instead, and one would probably have one's own namespace for one's own header, but that's not as foolproof as an unnamed namespace since a using directive for that namespace would then bring the using directive for the std namespace (or whatever other namespaces were involved in using directives in the custom named namespace) into scope.
    I see what you mean. You could still use the construct within header-only includes:

    Code:
    #ifndef DEMO_HPP_
    
    #include <string>
    #include <iostream>
    
    namespace demo {
    
    namespace {    
        
    using namespace std;    
        
    void fun(const string& arg)
    { 
        cout << arg << endl; 
    }
    
    } // namespace
    
    } // namespace "demo"
    
    #endif // DEMO_HPP_ not defined
    Code:
    #include "demo.hpp"
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string arg = "Hello World!";
        demo::fun(arg);
    }
    But of course if the user then decides to import the entire "demo" namespace:

    Code:
    #include "demo.hpp"
    #include <string>
    
    using namespace demo;
    
    int main()
    {
        string arg = "Hello World!"; // Yikes, no complaint from the compiler here!
        fun(arg);
    }
    Side effect being that the entire std namespace gets imported along with it!

    So yeah, maybe not the best approach.

  7. #7
    Registered User
    Join Date
    Sep 2018
    Posts
    23
    Umgh okay.. but I still didn't understand why using namespace shouldn't be used exactly? I don't even know why it even exists..

  8. #8
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    39
    Quote Originally Posted by Salem View Post
    >That's why editors have global search and replace features
    And we all know that a world without search and replace features would rightly be called HELL.

  9. #9
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    39
    Quote Originally Posted by Nwb View Post
    Umgh okay.. but I still didn't understand why using namespace shouldn't be used exactly? I don't even know why it even exists..
    Suppose there were two libraries you'd like to use in your code which both have a function called "reverse". If they didn't use namespaces then you'd obviously get a bunch of compile errors. So namespaces basically prevent name collisions. And it's entirely possible that the libraries just happen to choose the same name for their namespace of course, which is why the name chosen should ideally be as unique as possible.

  10. #10
    Registered User
    Join Date
    Sep 2018
    Posts
    23
    So writing "using namespace std" would search std for every function in my program? Since the command is not interfering with the functions I defined, I assume everything (that is defined by the user) is assumed to be in std namespace?

    So every library function belongs to std right?

    Oh and thanks to everybody who replied!

    So in short, I must not use using namespace std because it's not good if I want to have more than one namespace. I still can't think of why I would use more than one namespace but it kinda makes sense.

  11. #11
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    39
    Quote Originally Posted by Nwb View Post
    So writing "using namespace std" would search std for every function in my program? Since the command is not interfering with the functions I defined, I assume everything (that is defined by the user) is assumed to be in std namespace?

    So every library function belongs to std right?

    Oh and thanks to everybody who replied!

    So in short, I must not use using namespace std because it's not good if I want to have more than one namespace. I still can't think of why I would use more than one namespace but it kinda makes sense.
    Basically yes, when you declare "using namespace X" it imports everything from X into the current namespace (which is often enough the global namespace/scope).

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,207
    So writing "using namespace std" would search std for every function in my program? Since the command is not interfering with the functions I defined, I assume everything (that is defined by the user) is assumed to be in std namespace?
    No, things defined by the user are in the global namespace by default. In fact a user should never try to add something to the std namespace.

    So every library function belongs to std right?
    No, the only libraries that should use the std namespace are the standard libraries to implement the functions required by the C++ standard.

    So in short, I must not use using namespace std because it's not good if I want to have more than one namespace.
    Using or not using the "using namespace std;" has nothing to do with having more than one namespace. The "using namespace std;" line was really designed to make porting pre-standard C++ code to standard C++ easier. And realize that you always have at least two namespaces, the std namespace and the global namespace.

    I still can't think of why I would use more than one namespace but it kinda makes sense.
    You would use more than one namespace when you start developing real world applications that are more than just one or two user defined classes in more than one or two different files. And you will also start using namespaces when your functions and variables have similar/identical names in multiple libraries. Plus while many instructors allow indiscriminate use of using statements (and many other bad practices) most employers will be very disappointed to see these statements anywhere, except in very limited scope.

    You could still use the construct within header-only includes:
    But remember that headers should rarely, if ever, have executable code within the headers unless you are dealing with templates or inline functions.

    Suppose there were two libraries you'd like to use in your code which both have a function called "reverse". If they didn't use namespaces then you'd obviously get a bunch of compile errors.
    No, you will not always get a bunch of compile or even linker errors. This is what makes name collisions so hard to find and what makes proper use of namespaces extremely important. It is possible to have functions with the same name as long as each function has a different signature and if you mix up the parameters you may call an unexpected function producing unexpected results.

  13. #13
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    39
    Quote Originally Posted by jimblumberg View Post
    No, you will not always get a bunch of compile or even linker errors. This is what makes name collisions so hard to find and what makes proper use of namespaces extremely important. It is possible to have functions with the same name as long as each function has a different signature and if you mix up the parameters you may call an unexpected function producing unexpected results.
    I'd like to take this moment to blame my poor explanation on the lack of any tea or coffee in my system.

  14. #14
    Registered User
    Join Date
    Sep 2018
    Posts
    23
    For future readers, using "using std::cout" or likewise for any other function (ex: using std::endl) is also an option.
    Cheers

  15. #15
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    716
    Right, and you can do this at function or block scope as well, instead of the whole file:
    Code:
    void fn1()
    {
        using std::vector;
        vector<int> std_vec;
    }
    
    void fn2()
    {
        // vector<int> std_vec; // error
        std::vector<int> std_vec; // ok
    
        xyz::vector<int> xyz_vec;
    }
    Last edited by -Adrian; 2 Days Ago at 04:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 10-05-2016, 07:08 PM
  2. Replies: 4
    Last Post: 08-24-2014, 08:26 PM
  3. "is not a class or namespace name" error
    By blakeo_x in forum C++ Programming
    Replies: 13
    Last Post: 09-16-2011, 03:08 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread