Used to prevent name collisions when using multiple libraries, a namespace is a declarative prefix for functions, classes, types, etc.
namespace *identifier*(*opt*) { *declaration-seq* }inline namespace *identifier*(*opt*) { *declaration-seq* } /* since C++11 */inline(*opt*) namespace *attribute-specifier-seq* *identifier*(*opt*) { *declaration-seq* } /* since C++17 */namespace *enclosing-namespace-specifier* :: *identifier* { *declaration-seq* } /* since C++17 */namespace *identifier* = *qualified-namespace-specifier*;using namespace *nested-name-specifier*(*opt*) *namespace-name*;*attribute-specifier-seq* using namespace *nested-name-specifier*(*opt*) *namespace-name*; /* since C++11 */The keyword namespace has three different meanings depending on context:
using and followed by a namespace name, it forms a using directive, which allows names in the given namespace to be found by unqualified name lookup (but does not redeclare those names in the current scope). A using-directive cannot occur at class scope.using namespace std; is discouraged. Why? Because namespace std is huge! This means that there is a high chance that names will collide:
//Really bad!
using namespace std;
//Calculates p^e and outputs it to std::cout
void pow(double p, double e) { /*...*/ }
//Calls pow
pow(5.0, 2.0); //Error! There is already a pow function in namespace std with the same signature,
//so the call is ambiguous