Logo

using namespace std

Computerwelt

Subject: using namespace std;
From: "Phlip" <no(at)spam.com>
Newsgroups: comp.lang.c++

> I see this line all the time in this newsgroup, what does it do?
> ---
> using namespace std;

Do you remember the scene in Star Trek Old Generation where they could not get the hatch to a grain silo open, and when Kirk finally opened it thousands of tribbles rained down all over him?

Kirk represents your source file.

The grain silo represents all the header files your source file includes.

The tribbles each represents an identifier declared inside 'namespace std' in those headers.

Raining down all over Kirk represents all those identifiers polluting your local namespace.

'using namespace std' represents sliding the hatch open.

The purpose of the 'namespace' keyword is to prevent this pollution. You must keep all the tribbles in the grain silo, and only take down the one or two that you need:

using std::cout;
using std::endl;

Folks use 'using namespace std' in this newsgroup because trivial example code often uses it; the code is not large enough to have enough of its own identifiers to potentially conflict with the 'std' ones. But nobody should use 'using namespace std', and those who post sample code to this newsgroup should set a good example.