-6

Some people claim that using namespace std; is bad practice. Others say it's OK in it's place. But everyone agrees that putting it in a header file is verboten.

So dear reader, who can explain this from Bjarne Stroustrup's own web site.

http://www.stroustrup.com/Programming/Graphics/std_lib_facilities.h

He didn't even put it at the top where you might notice it, its about 50 lines down.

What's going on here? Are mischievieous hackers out to discredit BS by putting bad code on his web-site?

5
  • Well, it might be 50 lines down, but it is right after he gets done including his headers and using other namespaces. And it was educational work IMHO std:: makes things harder to read. And the easier it is the more understandable it is to the students.
    – Dair
    Commented Aug 20, 2011 at 20:21
  • Maybe it's just an example and not production ready code? Just a wild guess.
    – Kevin
    Commented Aug 20, 2011 at 20:23
  • 1
    When I was a kid my teacher told me it is terribly bad to do sqrt(-1), but now I use that everyday. the answer to your question depends on the environment you use. If I'm writing a code to do a simple job, e.g. some math issue, I will definitely using namespace std;, otherwise I will write code like std::sqrt. BS used that in a teaching book, so it is ok.
    – xis
    Commented Aug 20, 2011 at 20:43
  • -1: C++ is a huge, huge language. There's no reason to expose newbie students to namespaces from the start. The important thing to learn at the start is objects. Namespaces are just a hindrance at this stage. Once they learn the basics they can be taught that some of what they were using was bad form. Commented Aug 20, 2011 at 20:46
  • Even ignoring the context (as others have pointed out), I'm not sure I understand the "If Stroustrup does it, then it's not bad" logic. Even if one of the chief architects of the language does something commonly considered wrong, that doesn't mean it suddenly becomes a good idea. Commented Aug 20, 2011 at 21:05

3 Answers 3

24

Did you read the comment at the start of the file?

simple "Programming: Principles and Practice using C++" course header to be used for the first few weeks. It provides the most common standard headers (in the global namespace) and minimal exception/error support.

Students: please don't try to understand the details of headers just yet. All will be explained. This header is primarily used so that you don't have to understand every concept all at once. Revised April 25, 2010: simple_error() added

The idea is so you can get started in the language without having to learn about namespaces day 1 nor worry about which standard headers need to be included for what.

1
  • Exactly. Namespaces are not the most important aspect of the language. Objects are. Commented Aug 20, 2011 at 20:47
3

Seeing as this is a course header, I think students are supposed to include it and then use most of the standard library that way. I am surprised Stroustrup teaches it that way (it is, in my opinion, still bad practice), but it does mean that he has one less bit of syntax to explain to his students. I don't think any non-coursework applications (or, even worse, libraries) are supposed to include it, so it's arguably somewhat acceptable.

I still think it's very poor style, and that saying that standard things have to be prefixed with std:: would have been better.

0

Firstly, that's teaching material and therefore far from the same. Secondly- so what? Everyone makes mistakes or maintains legacy headers. And thirdly, just because you invented a language doesn't make you the permanent master of everything- especially when there's a Standards committee.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.