What does ‘using namespace std’ mean in C++?

Manish Kumar
3 min readFeb 15, 2017

--

Is it necessary, at all?

Is necessary or not?

Have you ever wondered why do we put ‘using namespace std’ on the top of our code in C++. What is namespace?, Is it really necessary to mention this, like we mention header files in our code. In this article we will try to get the answer of these questions.

First, lets understand what a“namespace” is!

What is namespace?

It is a container for a set of identifiers. It provide a level of direction to specific identifiers, thus making it possible to distinguish between identifiers with the same exact name. For example, a surname could be thought of as a namespace that makes it possible to distinguish people who have the same given name. For further details go to this link.

Why we need namespaces?

Assume there is two libraries named A and B, both have get_data function in it. You included these two libraries in your code. Now you call the get_data function, then compiler won’t know which one to call, and you’ll get an error. Now how we can fix this issue. In C lang we can fix by adding a prefix to start of every function like A_get_data and B_get_data,but is painful. The solution of this problem comes in C++ in the form of namespace. It is more flexible way of adding prefixes. So now you can call the functions as A::get_data and B::get_data where :: is scope resolution operator.

Now come to point, what does ‘using namespace std’ do???

Why we mention this in our code?

Now suppose in the above example, if you are going to use bunch of code of A library then its going to boring to add prefix every time while calling the functions of A library. Then what is the solution of this problem

What can i do?

here is the solution you can add “using namespace A” at the top of your file, and then just call get_data without using A prefix for the rest of code.

Is ‘using namespace std’ is necessary to mention?

The answer is big NO.

What really!!

The std namespace is special, The built in C++ library routines are kept in the standard namespace. That includes stuff like cout, cin, string, vector, map, etc. Because these tools are used so commonly, it’s popular to add “using namespace std” at the top of your source code so that you won’t have to type the std:: prefix constantly. It only make our task easy, It is not not necessary.

cout << “Hello world.” << endl;  // Here it is necessary to put ‘using namespace std’ on the top of code.std::cout << “Hello world.” << std::endl; // Here there is no need to put ‘using namespace std’ on the top of code.

Drawbacks of ‘using namespace’

Professional programmers considered that it is bad manners to put “using namespace” declarations in header files. It forces all includers of that header file to use that namespace, which might result in naming ambiguities that are hard to fix. This practice is called namespace pollution. Instead, always use the fully prefixed names in header files (std::string not string) and save the using declarations for the source files.

What You Learnt:

  1. What is namespace?
  2. Why we need namespaces?
  3. What does ‘using namespace std’ mean?
  4. Drawbacks of ‘using namespace’ .

Don’t forget to Recommend & Share :)

--

--