Thread: Thoughts about using namespace std?

  1. #31
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    It will tell you that the object is from the standard library. But since it is standard it kind of loses its usefullness.
    You are forgetting a namespace is a non-trivial matter and that it has scope implications. That is, this is not only a matter of preference. It needs to be weighted with the consequences. EDIT: and master5001 just named one example.

    But even still, I can assume that whatever has no :: is standard.
    You can't actually, because any name you define yourself outside of a namespace is not "standard". Consequently, when reading code the original programmer is making no distinction between his own names and those of the std.

    What will the std:: help you?
    Me the reader? Little probably. Depends on my knowledge of the language and how easily I can identify standard names at a glance and how much information I can retain while reading the code. Me the coder? Depends on several factors, but for the most part I can conceded to the fact it won't have any practical benefit in the short term.

    Question is if that is a reason to not write it. I tried to reason why it should be written on most circumstances. There's others I didn't find the need to repeat -- like Daved's call for consistency which is equally important.

    However the strong point I try to make on that post is that we tend to look at readability as only a matter of form. It isn't. For me, the presence of std:: doesn't make the code less readable. It in fact makes it more readable. So there is also the issue as to exactly how many find std:: invasive.

    Code readability on the other hand is much more about expressiveness, and while you can still make a case of std::, it will be a weaker one.
    Last edited by Mario F.; 09-24-2008 at 08:01 PM.
    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.

  2. #32
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    it sounds like this discussion is derailing into should a brace have its own line or not. i used to have the brace on the same line. but then i switched jobs where braces are on new lines. i hated it with a passion for about a week, but then i got used to it and now i don't mind.

    i think it's the same thing with std::. those who prefer one or the other could probably very easily be switched to the other given the time for them to adapt.

  3. #33
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I gather you didn't actually read the thread or it's a matter of readability. Either:

    a) we didn't make very readable posts, or
    b) you didn't make an effort to understand what is being said

    I put my money on b.
    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.

  4. #34
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    readability is subjective.

    edit: wasn't there a time when people actually liked hungarian notation?

  5. #35
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Hungarian is fine, in my opinion. I know the reasons people give for not liking it... and I'm not sure if they have enough bite to really make it an issue. Any naming convention is good, as far as I'm concerned. Be it in how you name variables or how you call functions I think consistency is good. With using namespaces ... I don't do it. I always feel better knowing exactly from where something is being called, function names, objects etc can't always be unique. Sometimes it's nearly obvious. But with programs interfacing to many libraries, namespaces for many different uses... it could be messy, but I prefer clarity. Anon's example above shows how messy it can be, but with a quick glance what type is the 'twomers_or_std' variable? Without referring to the top.

    Code:
    namespace twomers {
      template<typename _Ty,typename _Ax = allocator<_Ty> >
      class vector{};
    }
    
    using namespace std;
    using namespace twomers;
    
    
    int main( void ) {
      vector<int> twomers_or_std; // ?
    
      // No confusion
      twomers::vector<int> twomers_vector;
      std::vector<int> std_vector;
    
      return 0;
    }
    Last edited by twomers; 09-25-2008 at 11:30 AM.

  6. #36
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by twomers
    but at a quick glange what type is the 'twomers_or_std' variable? Without referring to the top.
    At a quick glance, the program would not compile even if the ambiguous statement was removed

    But what your example shows is that having two using directives in the same scope can make things confusing. It does not show that using directives necessarily introduce confusion, and even then confusion can always be avoided by explicitly qualifying names in conflict, or by switching to using declarations.
    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

  7. #37
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> At a quick glance, the program would not compile even if the ambiguous statement was removed
    It compiles fine on MSVC '03. I don't think it matters that my simple test case isn't an exact replica of the problem at hand, but it shows that using them globally can.

    >> But what your example shows is that having two using directives in the same scope can make things confusing.
    So take out the twomers namespace and make a class called vector and retain using namespace std.
    Code:
    template<typename _Ty,typename _Ax = allocator<_Ty> >
      class vector{};
    
    using namespace std;
    
    int main( void ) {
      vector<int> local_or_std; // ?
    
      return 0;
    }
    Same problem. Which vector is it?

    Edit: I'm not saying this is a good example or anything. It's a lousy one, in my opinion. But it does highlight potential issues with the problem, especially if you're attempting to do something 'standard' with a good name and are in the habit of using namespaces. Just showing potential for confusion.
    Last edited by twomers; 09-25-2008 at 11:58 AM.

  8. #38
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by twomers
    It compiles fine on MSVC '03. I don't think it matters that my simple test case isn't an exact replica of the problem at hand, but it shows that using them globally can.
    I just found it surprising that you used allocator without fully qualifying it, despite claiming that "With using namespaces ... I don't do it." Of course, there is also the missing header inclusion, but that's forgivable.

    Quote Originally Posted by twomers
    So take out the twomers namespace and make a class called vector and retain using namespace std.
    You still have two using directives in the same scope, except that one is implicit since it concerns the global namespace.

    EDIT:
    Quote Originally Posted by twomers
    Edit: I'm not saying this is a good example or anything. It's a lousy one, in my opinion. But it does highlight potential issues with the problem, especially if you're attempting to do something 'standard' with a good name and are in the habit of using namespaces. Just showing potential for confusion.
    I agree, though I think that by "using namespaces" you actually mean "using using directives or using declarations"

    My opinion is that if the using directive is within some restricted scope, there would generally be less of a problem since the fact that there is a using directive at work would be more obvious to the reader. On the other hand, I find that the opportunity to use using directives and using declarations is not that common, since I only use them when necessary, or to make a piece of code that uses the namespace name far too often more readable.
    Last edited by laserlight; 09-25-2008 at 12:24 PM.

  9. #39
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    As long as we're at it, why don't we use ::std::cout?

  10. #40
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Personally, I like to use using for very important common libraries, like std. Other lessly used libraries I will just type out. One advantage with using :: is on some ide's they have the code completion, which I think helps out greatly.

  11. #41
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> I just found it surprising that you used allocator without fully qualifying it
    Heh. I was just copying what intellisence was telling me std::vector used as template arguments. I figured it would make it more 'complete' in a sense.

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