Skip to content

What Does using namespace std Mean in C++

In this guide
  1. What the line does
  2. Why people warn against it
  3. What to write instead
  4. Is it wrong for a beginner

Short answer

using namespace std; tells the compiler to treat every name in the standard library namespace as if you could write it without the std:: prefix. It saves typing, so cout works instead of std::cout. The cost is that it imports every standard name, which can clash with your own, and learncpp.com recommends explicit std:: prefixes instead.

What the line does

Everything in the C++ standard library lives in a namespace called std. A namespace is a named group of names, which keeps different libraries from fighting over the same word. To use cout you normally write std::cout, where std:: says where to find it.

The line using namespace std; is called a using-directive. cppreference defines its effect on name lookup: for unqualified lookup of any name after the directive, until the end of the scope it appears in, every name from the namespace is visible as if it were declared in the nearest enclosing namespace that contains both the directive and the namespace. In plain words, after the line you can drop the prefix.

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello, world!\n";
}

The same program with no directive:

#include <iostream>

int main()
{
    std::cout << "Hello, world!\n";
}

Why people warn against it

cppreference states the risk: using namespace std; at namespace scope introduces every name from std into the global namespace, which may lead to undesirable name collisions, and using-directives are generally considered bad practice at file scope of a header file.

learncpp.com is blunter. Showing a program that starts with the line, it says that if you see this in a textbook or tutorial, the material is probably out of date. Its summary is that in modern C++, using-directives generally offer little benefit, saving some typing, compared to the risk.

A collision

learncpp.com gives an example. If you write your own function named cout in the global namespace and also use using namespace std;, a call to cout becomes ambiguous between your function and std::cout. The compiler fails with an ambiguous symbol error. The same happens when two namespaces each define a name such as x.

The standard library has a very large number of names, so the chance of a clash grows with every name you import. learncpp.com says the possibility of naming collisions increases significantly.

What to write instead

learncpp.com's advice is to prefer explicit namespace qualifiers over using-statements. Write std::cout and std::string. It also says that scoped using-declarations such as using std::cout; are acceptable in .cpp files after the includes, and that using-statements should never go in header files.

A using-declaration brings in one name, not the whole namespace:

#include <iostream>

using std::cout;

int main()
{
    cout << "Hello, world!\n";
}

cppreference also shows that a directive can be limited to a block:

{
    using namespace std;
    vector<int> v3;  // Same as std::vector
}

That limits the damage, because the names are visible only inside the braces.

Is it wrong for a beginner

The line is legal C++, and many beginner tutorials still use it. Nothing in the sources says it is an error. The advice is about habit: learning with std:: from day one means you always know where a name comes from, and you will not be surprised when you read other people's code. If your course or tutorial uses it, you can follow along, then switch to std:: prefixes in your own projects.

For what the rest of a first program means, see what does int main mean in C++, and for a plan of what to learn first, how to learn C++ as a beginner.

C++ Game: Segfault

Teaches C++ to complete beginners as a game where you tap one real line of code at a time.

Get it on Google Play

Free to play. Halls 1 to 4 are free with no time limit, and free players can watch optional rewarded video ads for bonuses. Segfault Pro, a monthly subscription or a 3-month pass paid once, opens hall 5 and up, unlimited practice sets and mock exams, with no ads.

Segfault teaches C++ to complete beginners as a game with 11 halls from variables to loops, functions and arrays. You tap one real line of C++ at a time and watch it run, and each line is compiled and run with a real C++ compiler. It does not teach you to write whole programs by typing, so use it alongside a tutorial such as the ones above.

Sources

  1. Namespaces - cppreference.com
  2. Using-declarations and using-directives - learncpp.com

Search the site