Thread: Should I avoid using namespace std?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Location
    Louisville Ky
    Posts
    6

    Should I avoid using namespace std?

    I have seen some various things around the forum and was wondering if 'using namespace std' should be avoided. I have seen some people claim that this is bad practice that it is better to preface your statements with std:: Would someome please explain this and which option is better?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You should avoid any using directive (e.g., using namespace std; ) or using declaration (e.g., using std::cout; ) in a header file, unless it is within some restricted scope, e.g., within the body of a function.

    Other than this, it is a matter of style: there are pros and cons, e.g., the presence of the using directive may increase the chance of a name collision even when used in a local scope, but then it also makes the code easier to type and read. On the other hand, fully qualifying the name might also make it easier to identify exactly which name it is, i.e., it makes the code easier to read in another way.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    It's an easy kludge to get pre-ANSI C++ up to standard.
    - remove the .h from all the standard C++ header files (iostream.h -> iostream)
    - prepend 'c' to the start of standard C header files (stdio.h -> cstdio)
    - add "using namespace std;"

    Job is nearly done.

    For any new code, a bit more finesse should be used.
    This is especially true if this is going to be a large project with multiple namespaces to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Location
    Louisville Ky
    Posts
    6
    Thanks a lot for the information. Apparently I had misunderstood some of stuff that I read previously. Seems like the using statement is OK to use as long as it is not in a header file, then extra caution should be used.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by pikuchan View Post
    Seems like the using statement is OK to use as long as it is not in a header file, then extra caution should be used.
    Not quite.

    It is generally inadvisable to have a using statement in a header file (except in restricted scopes, as mentioned by laserlight). It can be used with care in other contexts.

    That's a little different to your conclusion, IMHO.

    Personally, in real code, I never employ any using directive, unless it is the only option available. In practice, that means never employing "using namespace" and only rarely (eg in a class definition) even having a using directive.

    (Note that I don't view code I post in forums as "real code" - it's for illustrative purposes only )
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by grumpy View Post
    Not quite.

    It is generally inadvisable to have a using statement in a header file (except in restricted scopes, as mentioned by laserlight). It can be used with care in other contexts.

    That's a little different to your conclusion, IMHO.

    Personally, in real code, I never employ any using directive, unless it is the only option available. In practice, that means never employing "using namespace" and only rarely (eg in a class definition) even having a using directive.

    (Note that I don't view code I post in forums as "real code" - it's for illustrative purposes only )
    so if it is not really a good idea - sounds to me like it isn't a good idea at all
    why was it made then?
    I find it easier to declare using directive..reduces how much i use to type.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    The using directive was made in order to bring the millions of lines of existing code up to the new standard. What's a little extra typing if it helps produce clean code.

    Jim

  8. #8
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    cool. I still have a lot to learn!

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Eman
    I find it easier to declare using directive..reduces how much i use to type.
    That is the core of the argument that Sutter and Alexandrescu make for recommending the liberal use of using directives and using declarations in source files (but not in header files, or before #include directives). The problem with liberal use is really that name collisions become more likely, and you might make a mistake in assuming that a name is from one namespace when actually it is from another (e.g., because a certain header was not included).

    My preference is to use a single using directive in a local scope when many names from that namespace would otherwise need to be fully qualified in that scope. If there are names from more than one namespace that would qualify, I pick the namespace for which the using directive would increase the readability the most. This way, the possibility of collision is more or less limited to names local to that scope (e.g., local and member variables), names from the global namespace (which should be rare), and names from that namespace for which the using directive applies.

    If a specific name is used many times in a local scope, I tend to change the using directive to a using declaration. Using declarations see slightly more use though, e.g., to enable overloads of std::swap to be utilised, or to bring in names from a base class that would otherwise be hidden by overloaded names from the derived class.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    That's one thing that annoys me when I read Java code examples online. People never fully qualify Java classes and their examples don't even have the import statements, so you're left guessing which class their code uses. But it certainly makes code a lot more readable by not fully qualifying everything. It's a double edged sword I guess.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I completely agree with laserlight. It should by no means be considered a faux-pas, but it should be understood properly and used only when needed. And indeed, the use of using declarations is always a good idea, if only because it limits the possible collision space.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #12
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    I avoid the 'using' statement as much as possible, but to make my code more readable (not to make it easier to type!) I use it in some cases. I also use to add a special highlight to those 'used' identifiers.

    Code:
    using std::time_t;
    using std::make_pair;
    
    using kmlib::make_shared;
    using kmlib::make_intrusive;
    In one of my projects, made of several stand-alone apps and a common static library, I also spliced the common library namespace to the local ones, improving code's readibility.

    These special cases were introduced because of the 80-column rule I stick to.

    And yet another good side of the using declaration:
    If I decide to move and use boost's make_shared function, I don't need to change every single statement in my program.
    Last edited by kmdv; 10-21-2010 at 07:58 AM.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Eman View Post
    so if it is not really a good idea - sounds to me like it isn't a good idea at all
    why was it made then?
    I find it easier to declare using directive..reduces how much i use to type.
    Because C++ is a language that gives you the tools, but gives you the choice of where to put them to use. It doesn't stop you from shooting yourself in the leg, so to speak, unlike some languages like Java.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    uh, what is difference between using declaration and using directive?
    do they not all mean
    using namespace std; ?

    So i should only use namespace std, in some .cpp and not header files?

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >>uh, what is difference between using declaration and using directive?
    >>do they not all mean
    >>using namespace std; ?

    They could mean the same thing at the end of the day, assuming you only ever use std objects or classes. The difference is application. A using declaration will resolve one thing for you, until the directive is out of scope:

    using std::vector;
    using std::auto_ptr;
    using std::cout;
    using std::cin;
    using std::endl;

    A using directive will resolve everything in an entire namespace until it is out of scope. Now assume the scope is file scope. Namespaces become pointless if you want to ignore them all the time, in an entire file, and it leads to the problems everyone has tried to communicate to you.

    After all if the only way to distinguish an identifier is through its namespace, and you've assumed a particular namespace is being used either way, the compiler will not know which definition applies to the identifier. So you have options: Assume namespace foo is being used and resolve bar explicitly because it is used less often:

    using namespace std;
    boost::shared_ptr<object> pthing;

    Or resolve things all the time. Or painstakingly use only one namespace, ever. The last option will be hard, and not something you will always have the option to do.

    >>So i should only use namespace std, in some .cpp and not header files?
    Just think for a minute. You and another person implement some useful libraries that *I* want, but you share identifiers for a class. If you put a using directive in your header file, you're assuming, if I want to write code without namespace resolutions in it, that I'm going to be using your object instead of some other object. (You're making the directive before I can, because #include is processed before the source.) That might not make complete sense, but if I'm only linking your library for part of its code, and using another implementation for something else that you wrote, for some reason (maybe that particular class has security flaws or is otherwise FUBAR), all of a sudden I can't compile my code. I could easily be gracious and change my resolutions, but isn't that kind of rude? The source of the problem is between the two libraries.
    Last edited by whiteflags; 10-21-2010 at 06:52 PM. Reason: mentally swapped directives and declarations

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thoughts about using namespace std?
    By dwks in forum C++ Programming
    Replies: 40
    Last Post: 09-25-2008, 05:14 PM
  2. How to avoid typing std:: (scope resolution operator)
    By Sharan in forum C++ Programming
    Replies: 9
    Last Post: 04-30-2006, 08:25 PM
  3. using namespace std;
    By spank in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2006, 06:28 AM
  4. *sigh* Using Namespace Std; Woes
    By KneeLess in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2003, 04:41 PM
  5. using namespace std, What does it do?
    By Ben K. in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2001, 10:42 PM