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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > 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.

  2. #2
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    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.

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