Advertisement

Why won't STL work?

Started by August 04, 2000 12:36 PM
12 comments, last by Mike 24 years, 4 months ago
POINT: why should you minimize the call to "using"?

If you''re writing a library, you should enclose things in a namespace to avoid global namespace collision.

If you''re using a library, and none of your libraries collide, there''s nothing wrong with a global using. At least that''s been my experience.
name clashes are infrequent (ussually) but do occur, it''s only good practice to safeguard yourself from them.

consider this common example:

    namespace myNS{    int x;    int y;    int z;}int x;void foo(){    int y = 0;    using namespace myNS;    x++;       //error - myNS::x or global x (ambiguous)    y++;       //local y    z++;       //myNS::z    ::x++;     //global x    myNS::x++; //myNS::x duh <img src="wink.gif" width=15 height=15 align=middle>}void bar(){    int y = 0;    using myNS::x; //hides global x    using myNS::y; //error - y declared twice in bar()    using myNS::z; //ok}    


in foo(), the local y is altered, which may not be the intention and may therefore silently invalidate your code (uness of corse you want to alter the local y, but that is irrelevant).
in bar(), however, an error is generated which immediately alerts you to the ambiguity, allowing you to fix it!
Advertisement
Yes, that''s how namespaces work, and that''s the problem you want to avoid. BUT, if you have no conflicts, why is it bad form to do "using namespace X"? It just saves a lot of typing, from what I can tell.

I think it''s silly, as a style, to always use the std:: in front of every single function and class (especially since I use them a lot).
quote:
I think it''s silly, as a style, to always use the std:: in front of every single function and class


that''s what "using std::list" (etc) is for.

This topic is closed to new replies.

Advertisement