Thread: Namespace std;

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Namespace std;

    Hello all,

    I'm currently working through Accelerated C++ and it advocates the use of the std namespace by declaring each part, for example, rather than just stating using namespace std at the start it will state them seperately such as using std::cout and using std::endl.

    Which method is deemed better practise and do they both have specific purposes when they should be used exclusively?

    Thanks,


    Darren.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    This question has been asked an answered extensively on this forum and elsewhere on the internet. If you do a search you'll find a ton of interesting viewpoints on the subject.

    The basic consensus is that it comes down to personal preference. You should try to follow the style of the code you're working on, but in your own code it is up to you.

    There are some situations where it is bad to use a using directive or using declaration (like in a header file or above other #include's). You should be aware of those. There are rare instances where using directives or declarations can cause errors in your code, and there are also rare instances where they make your code better. I wouldn't worry about those at this point in your learning.

    So for now, I'd just use the style the book uses for consistency, and go ahead and do that search if you want more details.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also, since I know Accelerated C++ teaches one more bad practice, I will give you a link to it, so you don't fall into bad habits:
    SourceForge.net: Talko not remove parameter names - cpwiki
    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. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> since I know Accelerated C++ teaches one more bad practice

    What's the first bad practice it teaches? Certainly nothing in regards to what has been mentioned in the thread already.

    Besides, I don't necessarily agree with the arguments on that page either. Perhaps you should make it more clear that these are your opinions?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It doesn't add names to parameters in declarations. That's the bad practice it teaches.
    It may not have to do anything with the thread, but it serves to steer the OP away from some bad practices (as shared by a number of people on this board, I believe, as I have seen), and others reading the book, of course.
    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. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    First, that's your opinion. Absent other evidence I'd respect the opinion of Koenig and Moo over your opinion. The reasoning provided in the wiki page is interesting but not compelling.

    Secondly, you said, "one more bad practice" which implies there is a different bad practice being taught in the book. I'm just wondering what you think that is. Presumably it has something to do with this thread, otherwise you wouldn't have made that post here, but I don't see anything in this thread that the book teaches that is bad practice. The only possible choice would be the use of the std:: prefix, which I'd say is actually good practice, but at worst is a style choice.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Daved View Post
    First, that's your opinion. Absent other evidence I'd respect the opinion of Koenig and Moo over your opinion. The reasoning provided in the wiki page is interesting but not compelling.
    And others on the board. I have seen people agree with it and some mention it.

    Secondly, you said, "one more bad practice" which implies there is a different bad practice being taught in the book. I'm just wondering what you think that is. Presumably it has something to do with this thread, otherwise you wouldn't have made that post here, but I don't see anything in this thread that the book teaches that is bad practice. The only possible choice would be the use of the std:: prefix, which I'd say is actually good practice, but at worst is a style choice.
    Ah, I was referring to the issue on the use of use namespace or using directives, not that there is not other bad practice.
    The only two complaints I have about the books is the lack of names in declarations and sometimes non-descriptive names for variables. Otherwise it's one heck of a great book. It's the one I recommend to people.
    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.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> And others on the board. I have seen people agree with it and some mention it.
    That doesn't make it settled fact or even consensus opinion. When people as respected within the C++ community as Koenig and Moo teach something different in their critically acclaimed book, you need more than a few people agreeing with you on a message board before you can imply otherwise.

    There is a difference between a complaint about a style choice and a complaint about teaching bad practice. Yours was the former disguised as the latter. I feel it was necessary to point that out.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Point noted. Bad style choice then.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why std:: and not using namespace std?
    By swappo in forum C++ Programming
    Replies: 4
    Last Post: 07-03-2009, 03:10 PM
  2. std:: or namespace std
    By C-+ in forum C++ Programming
    Replies: 16
    Last Post: 12-04-2007, 12:30 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. need ; before using namespace std
    By wwfarch in forum C++ Programming
    Replies: 7
    Last Post: 05-11-2004, 10:42 PM
  5. Site tutorials
    By Aalmaron in forum C++ Programming
    Replies: 20
    Last Post: 01-06-2004, 02:38 PM