The C++ namespace in which all of the classes, algorithms, functions, and objects of the C++ Standard Library, including the parts incorporated from the Standard Template Library, are declared.

The C++ Standard Library defines a vast number of names, and it's a Good Thing that the Standard shunts them off into namespace std.

I'm especially grateful that names that were misused in the STL (such as map, set, and vector) don't pollute the global namespace.

Still, the move wrought a great deal of havoc among compiler vendors and people who had written billions of lines of C++ using Standard Library components before there were namespaces. For these people, there is the using directive.

namespace std is reserved for the exclusive use of the library.  People writing C++ programs are not allowed to declare new things in namespace std.

However, programmers are allowed to "reopen" namespace std in order to specialize class and function templates as needed.

Then again, they may not be. It's a matter of some debate.  The following construct (which compiles and works perfectly in a well-known compiler) may be perfectly legal; then again,  it may be undefined behavior.   I'm sure the C++ Committee members would consider it eeeeeevil.



#include <algorithm>


//
// header algorithm declares
// the generic template std::swap,
// which employs a third object
//

namespace three_xor {

  template <class I>
  inline void swap (I &i1, I &i2)
  {
   i1 ^= i2;
   i2 ^= i1;
   i1 ^= i2;
  }
} // namespace three_xor


namespace std {

  //
  // specialize std::swap for unsigned int
  // you could also specialize for
  // unsigned char, unsigned short int,
  // and unsigned long int
  //
  template<>
  inline void swap (unsigned int &i1,
                    unsigned int &i2)
  {
   three_xor::swap (i1, i2);
  }

  // Now, all swaps of two unsigned int variables
  // will use the three-xor swap.
  //
} // stop mucking around in namespace std,
  // for God's sake!

Log in or registerto write something here or to contact authors.