Recently i've noticed that if I don't include the 'using namespace std' in my program and just use 'std::cout' or 'std::endl' then my program still compiles just fine? Therefore if I use the 'std::' prefix is it even necessary to include the 'using namespace std'? Does the 'std::' prefix call the using namespace std? If someone can clear this up for me it would be greatly appreciated.
-
2I suspect it's a duplicate of this Q&A https://stackoverflow.com/questions/1265039/using-std-namespace – StoryTeller - Unslander Monica Apr 21 '19 at 06:27
-
1`std::stuff` just uses `stuff` from namespace `std`. – sjsam Apr 21 '19 at 06:28
-
2You may also benefit from getting a good [c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). That should definitely clear any all confusion you have. – StoryTeller - Unslander Monica Apr 21 '19 at 06:29
-
5Possible duplicate of [Why is "using namespace std" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – sjsam Apr 21 '19 at 06:29
-
2If you never use `using namespace std;` you'll be much happier. – Retired Ninja Apr 21 '19 at 06:30
-
2Possible duplicate of [Using std Namespace](https://stackoverflow.com/questions/1265039/using-std-namespace) – Igor R. Apr 21 '19 at 06:31
-
None of these proposed duplicates actually say what `std::` means, which seems to be the confusion. – Daniel H Apr 21 '19 at 06:35
2 Answers
In C++, ::
is called the "scope resolution operator", so std::cout
means "The variable cout
, which can be found inside std
".
The using namespace std;
directive means "For any name you can't find, also try looking it up in std
". Thus, if you explicitly say std::cout
(and equivalent for everything else in the std
namespace), then you don't need using namespace std
.
As others have said, it's usually considered bad style to use using namespace std;
, because a lot of the names in std
are things you might want to use elsewhere, for example std::count
. A compromise option is to instead say using std::cout;
, which will tell the compiler to look for cout
in std
but not anything else.
There's debate about whether to say using std::cout
or just write std::cout
everywhere you need it in your main C++ file, but most people believe that you should rarely if ever use using namespace
. If you're writing a header file, though, you should never use a using
directive at the top level because then every file which includes yours will get it, and they might want to use different names.

- 7,223
- 2
- 26
- 41
When you use:
using namespace std;
You're telling the program that anything that it can't find should be in the namespace std. For larger programs, this is a problem because you might be using multiple namespaces with functions that have the same name, so it's best to use std::cout
for larger programs.

- 75
- 10