0

This came up on another thread, rather that "pollute" that thread with a side issue, I'll start a new thread.

The issue is; Is it better to just to individually declare the
members of std to be used in a program, or is the global declaration "using namespace std" being unjustly maligned as a "global polluter".

For an authoritative answer, I went to the master himself, Bjarne Stroustrup, "The C++ programming Language" special edition, printed Jan 2006.

While he generally frowns upon the use, or more precisely,overuse of global declarations, due to the possibility of ambiguity, he does make a distinction between custom namespaces and the standard library.

The problem comes up if you start naming global variables associated with a namespace.

Morerover, unused names are NOT consiered errors
"When libraries declaring many names are made accessable thorough using-directives, it is a signifcant advantage that clases of unused names are not considered errors" (page 847).

All in all , it seems poor ol' using namespace std is just misunderstood and benign if used properly.

Who the heck want's to write std:: in front over everthing if you don't have to? It is however, recommended to do so with custom classes ie;
CustomClass::

That is the distinction!

For very small programs like one might write in college or HS I see nothing wrong with "using namespace std". But in larger commercial programs that probably should not be used due to many of the reasons you and others in the other thread have stated. A lot depends on the program you are writing and on your own personal preference. I hate seeing std:: in front of everything too and will either code "using namespace std" or "using std::cin" etc. at the top of the *.cpp file.

If the program is using more than one namespace and there is a possibility that the same symbol name is defined in each namespace, then there is no choice but to put the namespace in front of the symbol name each time it is used to avoid ambiguity.

4
Contributors
4
Replies
5
Views
10 Years
Discussion Span
Last Post by manutd
0

Personaly I individualy declare the std members I'm using (std::cout, std::vector, etc) for clarity in what I'm doing. I like including only what I'm using, rather than just dumping all of std into my program, as this defeats the purpose of using namespaces.

0

For small snippets, I often use std:: in front of all my objects, as it's simply not that much different than placing "using namespace std;" at the top of the program. And then the program looks more "correct", if you know what I mean.

For larger projects, I just add "using namespace std" whenever it's needed, although I should be more careful with ambiguity. I think after reading what Bjarne said, maybe I'm simply going to do the "using std::cin" method on large projects. After all, what's the harm in a few extra lines to protect the rest of the project?

0

>Is it better to just to individually declare the members of std to be used in
>a program, or is the global declaration "using namespace std" being
>unjustly maligned as a "global polluter".
Yes and no, respectively, but with one caveat. The big problem comes from dealing with the global namespace. The reason it's a big problem is because you can't expect everyone to know every name being used by the standard library. I've had to fix quite a few bugs that stemmed from using a standard name without knowing it. So this code is potentially dangerous for the same reason that a poorly named global variable is potentially dangerous:

#include <iostream>

using namespace std;

void foo()
{
  // Might unintentionally use standard names
}

int main()
{
  // Uses unadorned standard names
}

However, just as with global variables, the problem becomes virtually non-existent when you shrink the scope. The following is much better:

#include <iostream>

void foo()
{
  // Might unintentionally use standard names
}

int main()
{
  using namespace std;

  // Uses unadorned standard names
}

By restricting the scope in which you open a namespace, you can localize problems more easily. For clarity sake, it's always better to be explicit. So instead of dumping the bucket, you would have a using declaration for each name that you actually use.

0

I feel that only using something like the below best what was included in the file:

using std::cout;
using std::vector;
//etc...
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.