Tech Off Post

Single Post Permalink

View Thread: Should I make namespace std global?
  • User profile image
    The​Programmer

    Okay, so if I use using namespace std;, does that mean the program will compile everything named under namespace std? Many people recommend not to make the namespace std global, but some say it doesn't matter. But I usually do the second way, typing explicitly using::string, using::cout......until I read the newly released book from Andrei Alexandrescu (highly repected person in the field), the book named C++ coding standard. (ISBN: 0-321-11358-6)

    Here is a quote from the book:
    But here's the common trap: Many people think that using declarations issued at namespace level (for example, using N::Widge) are safe. They are not. They are at least as dangerous, and in a subtler and more insidious way.

    After reading this, I was thinking, am I doing the right way? So I thought I would ask people here how they are doing it.


    // Implementation file
    #include <iostream>
    using namespace std;

    int main()
    {
    // code
    return 0;
    }


    or should I do this:


    // Implemenation file
    #include <iostream>
    using::cout;
    using::cin;
    using::endl;

    int main()
    {
    //code

    return 0;
    }