Thread: What's "best practice" between 'using namespace std' and 'std::count/std::cin'

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    9

    Wink What's "best practice" between 'using namespace std' and 'std::count/std::cin'

    Hello again awesome team of coders......

    I know using (preprocessor?) 'using namespace std;' gives same result that 'std::count/std::cin' but since I noticed both syntax are used in books I wonder what is consider 'best practice' on this regard?

    Also noticed that some code editors (like atom, for example) default to 'std::...' syntax even if 'using namespace std' has been specified so I'm curious about what should be the more appropriate?

    Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Never put a "using namespace" directive in a header file, in the global namespace. That's the best practice. I prefer not to use "using namespace" at all, but that's more a matter of personal preference.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    9
    thanks.

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    It's not considered a good practice to be `using namespace` because of how C++ does overload resolution and you may get unintentional results if you're using a function that's defined in more than one namespace.

    Instead, it's typically accepted to enumerate your `using`s so you'd type:
    Code:
    using std::vector;
    using std::string;
    using std::cout;
    /* ... */
    To this end, anyone reading your code can now know _exactly_ what functions you're importing from the namespace.

    Edit:

    The worst was when I was trying to read some code and they did this:
    Code:
    using namespace folly;
    using namespace std;
    using namespace proxygen;
    Good luck looking up where each function comes from!
    Last edited by MutantJohn; 09-27-2016 at 04:16 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Instead, it's typically accepted to enumerate your `using`s so you'd type:
    Accepted by whom?

    IMO enumerating your using statements in the global space should be avoided as well. Keep all of your using statements as local as possible and prefer properly scoping the namespace with the scope resolution operator::.

    Jim

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I don't think there is really a best practice about this. The only hard rule was written in this thread here.
    Never put a "using namespace" directive in a header file, in the global namespace. That's the best practice.
    Like stated earlier it can be a personal preference, and the rules of name lookup can force your hand. Programs will also look like crap if you convince yourself that one shortcut is better than another shortcut.

    I don't really mind using namespace statements, but the only stance I can advocate for is doing what you have to do to get name resolution to work. You'll get into an internet argument if you care about this too much.
    Last edited by whiteflags; 09-27-2016 at 06:38 PM.

  7. #7
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Yeah, never do it in a header file in global namespace, it can mess things up, but otherwise... I'd stick to using it as locally as I could, but as long as you know it won't screw you - you're probably fine.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    using namespace std; always seemed like an easy cop-out for existing programs which suddenly had to deal with namespace aware compilers.

    An easy one-line fix, and your programs still compiled.

    But new code should take a much more refined approach to namespaces, especially when you have multiple namespaces in use at the same time.

    Sure, having to write
    std::cout << "hello world" << std::endl;
    is a PITA, but no one is ever going to be in any doubt as to what you meant.

    using std::cout; might be a comfortable half-way point if you use a lot of one particular aspect of a particular namespace.
    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.

  9. #9
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    using std::cout; might be a comfortable half-way point if you use a lot of one particular aspect of a particular namespace.

    Yeah, that was my thinking as well. Writing the entire name of something is incredibly annoying but the `using` stuff can help. Especially when your library is like 7 namespaces deep.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you have "long" namespaces then you might be better off considering a namespace alias rather than the using statements.

    Jim

  11. #11
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    My biggest gripe is that heavy usage of a library makes namespaces less than desirable. But that's obviously incredibly subjective.

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Another thing that can sway a new coder one way or the other is
    text books and colleges. Some of Deitel's older C++ books used to teach std::
    prefix throughout. Their newer versions all teach using namespace std; as the
    "standard", as they say. Not a major thing - but it can prevent some students new
    to the language learning about other ways to include namespace objects into their
    programs.
    Double Helix STL

  13. #13
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by Salem View Post
    Sure, having to write
    std::cout << "hello world" << std::endl;
    is a PITA, but no one is ever going to be in any doubt as to what you meant.
    Why, are there C++ programmers out there who doubt what
    cout << "hello world" << endl;
    means?

  14. #14
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I think Salem was talking about from a programmers "too much to type" sort
    of way, being having to put the std:: prefix before every namespace object etc.
    Double Helix STL

  15. #15
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    no one is ever going to be in any doubt as to what you meant
    On a side note, so this is pretty much offtopic... I found plenty of people that didn't quite know what std::endl does, so there's that.
    It doesn't just add new-line to the stream, it also flushes it, which may degrade performance if looped over... Which is why I use it only where I want to both - add a new-line character and flush immediately. "hello world\n" is shorter to write anyway.

    So yes, no doubt which endl is used here, if there are many, but there may be superficial knowledge on what it does anyway.

    /offtopic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-12-2015, 10:28 AM
  2. Question on "Jumping into C++" Chapter 13 practice problem 1
    By Ben Sturm in forum C++ Programming
    Replies: 9
    Last Post: 04-01-2015, 01:16 PM
  3. Replies: 4
    Last Post: 08-24-2014, 08:26 PM
  4. "Jumping into C++" Chapter 5 Practice Problem
    By Grae in forum C++ Programming
    Replies: 4
    Last Post: 09-04-2013, 01:46 PM
  5. Replies: 18
    Last Post: 03-07-2013, 06:55 AM

Tags for this Thread