Thread: Thoughts about using namespace std?

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> But I think that regardless of whether you have using namespace std; or not you can still type std::map
    True, but then there would be no point to the using directive.

    >> Can you use both styles?
    Use a using declaration (e.g. using std::cout) if you want to only bring in some of the names. Consistency is important when determining a coding style, so it rarely makes sense to do both in the same place.

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by anon View Post
    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.)
    No worries... just type xor:
    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << "Look " << (1 xor 1) << " wow!!\n";
    
        return 0;
    }
    You can do the same thing in C99 by including iso646.h.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by C_ntua View Post
    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?
    Oh yes, it's possible. The compiler won't care if you use the full prefix std:: even if you use using namespace std.

    I like to explicitly specify namespaces because of one fact: to me, they are a way to organize stuff. Therefore I can put a function named Allocate (which is pretty unambiguous, no?) into a namespace Memory (now it's not unambiguous), and explicitly type Memory::Allocate(). It adds clarity to what function it is and allows you to group them together.
    Who is to say, then, that you should not do the same for std? Prefixing std:: will let you know it's a standard function.

    And if the namespace is too long -> I::Am::A::Very::Long::And::Totally::Unnecessary::N amespace, just use a using declaration -> using I::Am::A::Very::Long::And::Totally::Unnecessary::N amespace = MyNSpace (or is it the other way around?).

    It's my best argument for explicitly using namespaces! Does that help?
    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.

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And if the namespace is too long [...], just use a using declaration
    I prefer namespace aliases, which is what I think you want here:
    Code:
    namespace fs = boost::filesystem;
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh yes, that's what they're called.
    Namespace aliases.
    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.

  6. #21
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Quote Originally Posted by dwks View Post
    Someone who is pro-"using namespace std" said this:

    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.
    Unlikely, though you can advocate a safer use of the directive by restricting its scope:
    Code:
    #include <iostream>
    
    int main()
    {
      using namespace std;
    
      //...
    
      cout<< x;
    }
    If your friend complains that he has to type it too often, you can use that to suggest more regular use of namespaces for program organization:
    Code:
    #include <iostream>
    
    namespace mystuff {
      using namespace std;
    
      void foo()
      {
        //...
      }
    
      void bar()
      {
        //...
      }
    }
    
    int main()
    {
      //...
    }
    My best code is written with the delete key.

  7. #22
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I got used to typing std:: at first, but when I started to use algorithms more often, I found that it has the tendency to add large amounts of noise.

    Code:
    std::vector<std::string>::const_iterator it = std::find_if(strings.begin(), strings.end(), std::bind2nd(std::greater<std::string>(), "eek"));
    Ok, the container might be typedeffed, and the whole thing could be spaced out over multiple lines, but this still leaves 20 characters (and 15 what might be on a single line) that contain practically no useful information. (And with using directive, this particular line might fit into the window on a single line.)
    Last edited by anon; 09-24-2008 at 08:53 AM.
    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).

  8. #23
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I came into the habit of std::ing everything cause I read someone with more posts than I say it was good; which is a really bad way of coming into a habit, now that I think of it. In light of this topic does anyone find this amusing?
    Code:
    C Board > General Programming Boards  > C++ Programming  >  Thoughts about using namespace std?
    >> Ok, the container might be typedeffed
    I wish I did that in a project of mine... I had a global std::map<std::string,std::map<std::string,std::vec tor<std::string> > > over 6k lines of code and didn't think to typedef it (still haven't), and I had to change it. I find that you just get used to typing std::. Nearly feels wrong writing string in code without it to me.
    Last edited by twomers; 09-24-2008 at 11:10 AM.

  9. #24
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    i got into the habit of std:: everything. i think a good compromise is to have:
    Code:
    using std::cout;
    that way you don't have to type std:: all the time and helps avoid naming collisions.

  10. #25
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by bling View Post
    i got into the habit of std:: everything. i think a good compromise is to have:
    Code:
    using std::cout;
    that way you don't have to type std:: all the time and helps avoid naming collisions.
    Agreed. I prefer to use using in a less encompassing manner just as you have in your example.

  11. #26
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Of course, the result of that sometimes is that you type a lot just to use cout or anything else once in the source file

    And the thing is, if the chosen name (e.g cout) just happens to collide with anything, you'll still get a surprising error.

    As I see it namespaces give you an opportunity to choose whether you need to disambiguate between names or not. Even with the using directive, you can still resolve naming conflicts in source files, should they arise. The namespace isn't irreversibly polluted. (But for beginners it is probably recommended to not use this freedom.)

    -------
    I'm no expert in Python, but compare it with the similar thing in Python: from somewhere import * (make names in "somewhere" visible without the module name):

    Code:
    >>> from math import *
    >>> def abs(x):
    ...     print "my abs"
    ...     return x < 0 and -x or x
    ...
    >>> print abs(10)
    my abs
    10
    >>> print math.abs(-10)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'math' is not defined
    >>> del abs
    >>> print abs(-10)
    10
    It seems that my abs shadows the abs function in math completely, and that there's no way to access the math one unless my version is removed from the interpreter.

    In C++ I could always type either ::abs or math::abs.
    Last edited by anon; 09-24-2008 at 04:48 PM.
    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).

  12. #27
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    For me, as a rule of thumb I never use using * until I find that I have typed something like 45 times. Ironically enough, usually immediately after adding the using * I only use * like twice. So you are right... I just don't necessarily do it very often. Even in my examples I rarely utilize using. I just write

    Code:
    std::cout << my_string << ' ' << my_int << std::endl << my_other_string << ' ' << my_other_int << std::endl;
    I have not yet found the typing of five letters to be counterproductive. Plus I typically use the c standard io library more than iostreams. Though according the the standard, technically one needs to be typing out std:rintf() too... But what are you gonna do.

  13. #28
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by dwks View Post
    Someone who is pro-"using namespace std" said this:

    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.
    A major part of code readability is not an issue of text obscurity, but instead of code interpretation. In that sense, std:: is more readable than the lack of it because it doesn't remove from the reader the ability to immediately recognize the object namespace. Code semantics is preserved.

    Readability is also a task of the reader as much as it is of the coder. As a programmer I certainly should strive to make my code readable and understandable. But as a reader I'm not suddenly free from any obligation. It's my responsibility to actually make an effort. Certainly I cannot expect C++ to read as a pocket novel. In fact as a reader I should be suspicious of oversimplification. Any code that seems to do so, should trigger my senses into sharp mode and make me read more slowly.

    On the other hand, if I'm reading code with the task of maintaining it, the issue of code readability is not one of form or style anymore, but one of semantics. Code readability here will be more an issue of my ability with the programming language, than it will be about the original programmer choices to make my task easier.

    Necessarily, I think the using directive as an evil thing when placed at global scope. Even more so when placed on a header file where the tool suddenly becomes a weapon. As Prelude suggested there's better uses to the using directive and even so it should only be done in context; If the local code to where the directive is applied makes heavy use of the namespace name, this heavy use interferes with normal flow of readability and there's no risk of future code to introduce nameclash issues.

    One final note goes to the application. I cannot consider the using directive useful in a small scale application. If the issue is typing less I suggest the programmer to not code in C++ but choose some other language. In a small scale application, such aim can only be perceived by me as lazyness. But in any case, even if i'm overstating, the programmer better get used to write a lot, and not to write less. That's just one of the aspects he will have to deal with in C++ if he chooses this to be his language. On the other hand, on large scale applications where it's conceivable for this tool to have some form of use, that's exactly where it's use may become more damaging and where its hould be avoided except on local scope and for those situations I suggested.

    In any case I would prefer if the programmer understood std:: as a normal part of his C++ lexicon.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #29
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    >>A major part of code readability is not an issue of text obscurity, but instead of code interpretation. In that sense, std:: is more readable than the lack of it because it doesn't remove from the reader the ability to immediately recognize the object namespace. Code semantics is preserved.

    Yes, but the object namespace is not more important than the object itself. When you right namespace:bject you read two things. When you write object you read one thing. Having more information is not always good. You might not care from which namespace it belongs. Especially it that namespace is well known, like std.

    What will the std:: help you? It will tell you that the object is from the standard library. But since it is standard it kind of loses its usefullness. I would like to know from which namespace an object is if you have 100 manually defined namespaces. But even still, I can assume that whatever has no :: is standard. So especially for the std namespace I don't find it bad to be included, even if it is the only one you use.

  15. #30
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    C_ntua, a primary example of Mario F.'s points is the string class. "Which string class?" you are probably asking. The answer is simply "good question." There are oh so many different ones in so many different libraries. It is a very common name for a very commonly needed type of class. Few are ever overly interchangeable. So if you are writing code that uses the STL and wxWidgets and Irrlicht, for example (i.e. you are writing a platform independant game) you have three separate libraries each with their own string class.

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