Advantages/Disadvantages of "using namespace std;"

Intro

The functions in libraries only become available to use once they’ve been declared as “included”, e.g. to include the functionality of the input/output stream:

#include <iostream>

In order to use the functions in the library, we first type which namespace they belong to, e.g.:

std::cout << "Hello, World!" << std::endl;

This is a bit tedious, so we can declare underneath our included library that we are using the namespace which the functions belong to, e.g.:

#include <iostream>
using namespace std;

Advantages:

Instead of typing out the namespace a thousand time, the function can be typed without the prefix std::

Disadvantages:

Calls to functions can be ambiguous if they are defined in more than one place.
To disambiguate, we use the prefix NameOfNamespace:: before the function. This is less risky when using multiple libraries.

1 Like

Privacy & Terms