Advertisement

polluting the global namespace?

Started by March 24, 2000 09:30 AM
3 comments, last by mordell 24 years, 7 months ago
My question revolves around the std namespace. I know that in my trusty C++ reference ,"C++: The Complete Reference"; H. Schildt, it states that if you want to use anything from the std library, you have to include its header and bring into scope by the using namespace statement. e.g. #include < vector > using namespace std; I could then now use the vector class by: vector < int > ints; However, for a year or two I have been doing it a different way, as I was originaly told that "using namespace std pollutes the global namepace" and I am not sure what that means. Back before intellisense came along (MSVC++), I used to declare and use a vector like: #include < vector > using std::vector; vector < int > ints; Because I didn't want to "pollute" the global namespace by bringing the whole std namespace into scope. By polluting the global namespace, I envisioned a heavy fog of data types, floating around causing all sorts of collisions and other problems, acid rain, etc.. However, if I just type std:: (without including any headers or using statements) I see all the members of the std namespace. Of course, without the proper header and using statement, the code won't compile, but intellisense shows 'em to me anyways. So, my question: Is there any difference from the two declarations mentioned here: //: declaration one #include < vector > using namespace std; vector < int > ints; //: declaration two #include < vector > using std::vector; vector < int > ints; Are there any issues that I am not aware of when using one form vs. the other? I have consistently used the second form for quite sometime now with no notable ill effects. Thanks! -mordell ps. modified to make use of "<>" .. Edited by - mordell on 3/24/00 10:50:53 AM Edited by - mordell on 3/24/00 10:52:54 AM
__________________________________________

Yeah, sure... we are laughing WITH you ...
Yes, there''s a difference.

If you''ve included any other standard header along the way (say &ltlist>), the first method will move all of the symbol names from that header into the global namespace. So, if you had a program like this:

#include &ltvector>#include &ltlist>using namespace std;vector&ltint> vec;// now I want my own class called "list"class list{// ...}; 

This won''t work, because "list" is already a defined class in the std namespace--you''ll get one of those redefinition errors. That''s the whole point of namespaces--it lets you use names that other libraries already use by keeping them separate.

I didn''t know that you could do "using std::vector". I thought if you didn''t want to include the whole namespace, you had to declare it with your variable:

#include &ltvector>

std::vector&ltint> vec;

So, I learn something new today. Thanks!
Advertisement

Yes, this part I understand.

quote:
This won''t work, because "list" is already a defined class in the std namespace--you''ll get one of those redefinition errors. That''s the whole point of namespaces--it lets you use names that other libraries already use by keeping them separate.


I am not so much worried about collisions ( I frequently use namespaces to avoid them ), as I am in trying to understand the differences (if any) of declaration.

Like what you mentioned here:

quote:
I thought if you didn''t want to include the whole namespace, you had to declare it with your variable:

#include &ltvector>

std::vector&ltint> vec;



Thats pretty much the same as saying:

#include &ltvector>

using std::vector;

isn''t it?

If so, does this also keep from polluting the global namespace? What exactly is polluting the global namespace?

Is polluting the global namespace just having multiple definitions like you mentioned with the list class, e.g. polluting the namespace with multiple definitions of class list?

Its not really a big deal...its just one of those things that starts nagging ya and gets hard to let go ..

Thanks for the input!

-mordell









__________________________________________

Yeah, sure... we are laughing WITH you ...
Not really the same. For instance, one of our libraries has a "string" class (don't ask). So in the same function we could do this:

#include &ltstring>
#include &ltmylib>

std::string str_std;
mynamespace::string str_mine;

I think if you were "using std::string", the default string would be std::string. You should still be able to use mynamespace::stg, but you'd have to declare it explicitly.

"Polluting the global namespace" means dumping your symbols into global scope, be they functions or classes. This is to be avoided both to limit collision and to better organize things. In general, if you have to give something global scope, it means you haven't figured out the right class to put it under or you don't have your class structure designed such that there is an appropriate class that fits. Plus, in the case of libraries, you can seriously screw up your clients if you have no namespace associated with your symbol names, forcing collisions.

Edited by - Stoffel on 3/24/00 5:36:22 PM

mucho gracias!

I guess I have avoided that bullet by not calling my own string class "string" and such.

thanks for explaining!


-mordell
__________________________________________

Yeah, sure... we are laughing WITH you ...

This topic is closed to new replies.

Advertisement