Thread: Thoughts about using namespace std?

  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057

    Thoughts about using namespace std?

    Someone who is pro-"using namespace std" said this:
    Code is much more readable if the namespace is not specified with each call but simply specified at the beginning of the file. As you mentioned, there is a benefit to specifying the namespace with each use. However, the cost is higher than the benefit.
    Does anyone have any thoughts as to how I might convince them that
    Code:
    std::cout << x;
    is better than
    Code:
    using namespace std;
    cout << x;
    ?

    I guess I shouldn't say that they are pro-"using namespace std". They're just in favour of making code as readable as possible.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I cannot give you a general answer to this question. I typically just keep typing std::whatever until I am sufficiently fed up with doing it and opt to utilize the using keyword. However, I do not like to rid my code of namespaces too much since they often give a little more clarity as to what you are doing. I think the answer is not only implementation specific but a matter of opinion as well. I often continue to type std:: in instances where others may not. Just don't put any using keywords in a header, and you will keep most folks happy.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Convince them that 6 is better than 1/2 a dozen? Probably not.

    But, you can make your points for why you prefer one over the other. I don't think "cost" has anything to do with it at all. The amount of time it takes to type "std::cout" versus "cout" is not measurable in the scheme of things.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Particularly since if you constantly type std::cout you are typing it without even thinking too hard about it. I say trading off between the two is more of a cost issue than anything. I prefer environments where I have the freedom to decide my style on my own. Though how often do things work like that...

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In C++ Coding Standards, Sutter and Alexandrescu actually recommend using using directives in source files, after all the includes, on the basis that it improves readability and is precisely a main reason for namespaces (as opposed to name prefixes, presumably). Koenig and Moo do not even mention using directives in Accelerated C++ (or they only give it a brief mention, I have forgotten the exact details), and instead show the use of using declarations.

    I agree with master5001: the rule that everyone agrees on is that one should avoid using directives and using declarations in header files (except within some restricted scope, if it is necessary). Personally, I tend to only use them at function scope to reduce "code clutter" when I feel that it improves readability, or at class scope when necessary.

    Oh, and on Meyers' recommendation, I often have a using declaration for std::swap when implementing swap functions, the idea being that if there is a non-member swap available, it will be used, otherwise the generic swap will be used.
    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

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Hmm, well, thanks for the comments.

    I know it's just a matter of opinion, sort of like indentation styles. I was just hoping that someone might be able to come up with another argument besides mine, which is "that's what namespaces are for, to avoid naming collisions".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well like indentations there may not be an easily defined "right way" but there are categorically wrong ways. I mean... if your code starts to become very difficult to follow because your usage of namespaces (not just the standard namespace) is just plain cluttering, there is a problem.

    Example:
    Code:
    my_buddy_ricks_lib::os_independant::types::my_float x = 0.0f, y = my_buddy_ricks_lib::os_independant::constants::pi;
    mylib::os_independant::graphics::renderer::drawing2d::draw_box(x, y);
    Right? I mean it is simply two variables declared then used in a single function call, however at first glance it looks like someone accidentally paste a linker warning into their source code or something.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by master5001
    Right? I mean it is simply two variables declared then used in a single function call, however at first glance it looks like someone accidentally paste a linker warning into their source code or something.
    That was funny

    That said, a namespace alias might be a reasonable alternative to using directives and using declarations in the example.
    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

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    As for myself I hate symbols that require shift. But I hate symbols in general. For me I prefer the Haskell style add x y rather than add(x,y) and the tabs instead of the brackets. So I would go for the using keyword. But that is a bad reason I suppose :P

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    As for myself I hate symbols that require shift.
    You lucky bastard When I need the xor symbol, I type Alt + 94 (on a laptop without a numeric keyboard)... (Actually, it seems I can type two of them with another key combo.)

    Anyway, using directive in source files doesn't mean that you cannot solve naming conflicts. So if you don't need the constant remainder that cout comes from std it should be quite OK.

    In headers, I understand, it may put the namespace in effect in any other headers that are included after this.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Personally, if I kept using using I would forget what namespace cout comes from within a program or two. That is just me...

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    how about a compromise?

    Code:
    using std::cout;
    using std::endl;
    
    //for everything that's often used
    That way the global namespace is not polluted too much, and you don't have to type "std::" over and over again.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There are two main reasons that I prefer always typing std::.

    First, I prefer the consistency. Since I use standard names in header files and follow the rule to not use using directives or declarations in header files, I like to be able to switch to the source file and see the type look the same there as it does in the header. It takes extra mental effort to know that map is the same as std::map.

    Second, I like making it obvious where the type comes from. Any type that includes the std:: prefix is from the standard library. If it has boost:: then it is from boost. If it is from some other namespace, then that is obvious as well. This is especially true when your variable names use lowerCamel or the lower_with_underscore style that the standard library uses, and your class types follow a different convention. Using standard library names without the namespace makes it harder to discern that they are types.

  14. #14
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Can you use both styles? Like using the using namespace std but still using types like std::map. For example, maybe you want to do that only to be able to write cout instead of std::cout, but all the rest you want the std:: . Will it be completely wrong? Is it bad practice?

    I also use a laptop and the left shift... they made it small. And yes, dear manufacturer there is a reason shift is a big button in the first place (damn Plaisio)

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It takes extra mental effort to know that map is the same as std::map.
    But I think that regardless of whether you have using namespace std; or not you can still type std::map if that is one of the few things that cause you mental effort

    Anyway, I tend to typedef containers, so the code would really seldom contain std::.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using namespace std error
    By Kayoss in forum C++ Programming
    Replies: 0
    Last Post: 04-26-2006, 01:56 PM
  2. using namespace std;
    By spank in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2006, 06:28 AM
  3. using more then one namespace? std and system? for a console app
    By fingerlickin in forum C++ Programming
    Replies: 7
    Last Post: 08-10-2005, 11:08 AM
  4. using namespace std, What does it do?
    By Ben K. in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2001, 10:42 PM
  5. What is "using namespace std; "
    By Engel32 in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2001, 07:23 AM