Note that namespace is a packaging concept, not a type. That is, one cannot create instances of a namespace. A namespace is a sophisticated mechanism for packaging libraries. If all the internals of a library are accessible, the packaging is no more than a bag of items. Generally, there are a few items that a library intends to present to the outside world, and everything else is for its own internal use. The few items to be exported cause much less name clash than exposing the entire namespace. All names other than operator delete and operator new in the C++ library headers are defined in the std namespace, or in a namespace nested within the std namespace. Including a C++ library header does not introduce any library names into the current namespace. You refer to the name cin, for example, as std::cin. Alternatively, you can write the declaration:
using namespace std;
(A using declaration allows us to access a name from a namespace without the prefix namespace_name:: )
Include the standard header <cstdlib> to effectively include the standard header <stdlib.h> within the std namespace.
#include <stdlib.h>
namespace std {
using ::size_t; using ::div_t; using ::ldiv_t;
using ::abort; using ::abs; using ::atexit;
using ::atof; using ::atoi; using ::atol;
using ::bsearch; using ::calloc; using ::div;
using ::exit; using ::free; using ::getenv;
using ::labs; using ::ldiv; using ::malloc;
using ::mblen; using ::mbstowcs; using ::mbtowc;
using ::qsort; using ::rand; using ::realloc;
using ::srand; using ::strtod; using ::strtol;
using ::strtoul; using ::system;
using ::wcstombs; using ::wctomb;
#if __IBMCPP_TR1__
namespace tr1 {
using ::lldiv_t; using ::llabs; using ::lldiv;
using ::atoll; using ::strtoll; using ::strtoull;
using ::strtof; using ::strtold;
}
#endif
}
There is one case in which we should always use the fully qualified library names: inside header files. The reason is that the contents of a header are copied into our program text by the preprocessor. When we #include a file, it is as if the exact text of the header is part of our file.
If we place a using declaration within a header, it is equivalent to placing the same using declaration in every program that includes the header whether that program wants the using declaration or not .In general, it is good practice for headers to define only what is strictly necessary.