namespaces

closed account (4Gb4jE8b)
i just read the article on namespaces
http://www.cplusplus.com/doc/tutorial/namespaces/
And i've heard/read that "using namespace std;" can screw things up, and should not be used in professional work. The article above does not reference this at all so now i move here to the forums. Why is using namespace std; bad practice both technically and professionally speaking?
When you use a namespace, you're polluting the global namespace, opening up possibilities of redefinition if you use multiple libraries that have functions, variables, types, or classes of the same name (which can happen). Does that answer your question?

-Albatross
closed account (4Gb4jE8b)
yep thank you albatross!
closed account (z05DSL3A)
about namespace using declarations
http://www.cplusplus.com/forum/beginner/9181/#msg42419
Last edited on
It's particularly bad in a header file. If you define some class of functions or the like in a header file, and you put using namespace std at the top, anyone who ever includes your header file suddenly has a global using namespace std in their code, and they don't even know.

There is an unspoken assumption when using someone else's header files for class definitions etc that it will not break all the rest of their code. When was the last time you actually read one of the standard header files? It's just assumed that including them will not trash everything, and they will provide only the functions and objects the documentation states they will. If they also drag in the entire std namespace, you've suddenly added a shedload of objects; objects with names that people may well already be using in their code (min, max and count spring to mind as classic namespace collisions I have seen).

This is something of a pet peeve of mine, as at some point in the last decade using namespace std suddenly started turning up at the top of every source file written by a beginner (I exaggerate a little, but I'm sure we've all seen it; one header file and two cpp files, all three having this stuck at the top like some kind of ritualistic magic totem). I never knew where it came from; it seems as if spontaneously a bunch of teachers and books all started adding it to the top of everything, with minimal instruction on what it actually did, leading to some kind of reflex action whereby people now don't even notice themselves writing it.

I don't blame people doing what they've been told to do by authority; I would, however, like to get hold of whoever started teaching without providing understanding and ask them to stop :)

I shall shut up now. Thanks for listening :p
Last edited on
you dont necessarily need to blow the namespace out of the water sorta speak or write std:: a hundred times either . you can just write like below and just open up the ones you are using. im just posting it because nobody mentioned this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <iostream>
#include <stdio.h>

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

int main(int argc, char * argv[])
{

        cout << "test cout and endl" << endl;

        // keep window open
        getchar();
 
       // implicit return 0;

}
}



Last edited on
Topic archived. No new replies allowed.