Namespace std

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • seforo
    New Member
    • Nov 2006
    • 60

    Namespace std

    Hi Guys,

    Is a reason why people sometimes don't use namespace std as it is i.e
    using namespace std;

    instead they opt for something like:
    using std::cout;
    using std::cerr;
    using std::endl;
    using std::showbase;
    using std::noshowbase ;
    using std::hex;
    using std::dec;

    Is any difference?
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by seforo
    Hi Guys,

    Is a reason why people sometimes don't use namespace std as it is i.e
    using namespace std;

    instead they opt for something like:
    using std::cout;
    using std::cerr;
    using std::endl;
    using std::showbase;
    using std::noshowbase ;
    using std::hex;
    using std::dec;

    Is any difference?
    As far as my knowledge is concerned, instead of using the whole std library, using std::xyz; means you need only that specific part of std library.

    Regards

    Comment

    • Darryl
      New Member
      • May 2007
      • 86

      #3
      It's because they don't want to pollute the global namespace. Namespaces are there to help prevent collision of identifiers from different libraries/code etc. Common names like count, max, min are used in a lot of different code. Those 3 are also part of the std:: namespace. If your program uses count for example, then you've got an error if you use "using namespace std;"

      using namespace was really only included so legacy code didn't break and people like Stroustrup discourages its use in new code.

      Slightly better, you can just pull in certain identifiers from a namespace instead of all of them, for example using std::cout, if you plan on doing a lot of output, but really this is also discouraged.

      Comment

      Working...