hsf-training / cpluspluscourse

C++ Course Taught at CERN, from Sebastien Ponce (LHCb)
Apache License 2.0
169 stars 65 forks source link

Discuss "using namespace std;" #479

Closed hageboeck closed 1 year ago

hageboeck commented 1 year ago

Add a slide on using namespace std and when not to use it. The slide is at the end of the namespace chapter in the advanced course.

I'm not very sure about the placement of the slide. We have discussed scopes and namespaces, but almost no std stuff yet. Also header/implementation haven't been discussed. I tried not to use a lot of std knowledge, but it's unavoidable. Can you think of a location later in the course where this slide would make sense?

Fix #461

bernhardmgruber commented 1 year ago

I'm not very sure about the placement of the slide. We have discussed scopes and namespaces, but almost no std stuff yet. Also header/implementation haven't been discussed. I tried not to use a lot of std knowledge, but it's unavoidable. Can you think of a location later in the course where this slide would make sense?

Since the rule applies more generally to using other namespaces in headers as well (std is just the prime example), I think it's fine that we did not discuss the standard library in detail yet.

The chapter on scopes and namespaces is the only place where we discuss using namespace ..., so I think it is a good place to discuss using namespace std; at the end!

sponce commented 1 year ago

Honestly I would not mention this at all, or if I would it would be to strongly recommend to NEVER use using namespace std. To me it's certainly not "OK in implementation files or inside scopes". You get the same clashes and vicious bugs as anywhere else.

As a correlate we should also strongly recommend to not use at all using namespace. Such a need is a clear indication of a flow in the hierarchy of namespaces put in place. If you're in the right namespace, you won't need it.

bernhardmgruber commented 1 year ago

The thing is, people at the JLab course asked about this specifically during the morning lecture and we discussed it. So I think we should have a slide about.

As a correlate we should also strongly recommend to not use at all using namespace. Such a need is a clear indication of a flow in the hierarchy of namespaces put in place. If you're in the right namespace, you won't need it.

I agree with this philosophy and it's usually also my practice. So maybe we should drop the \item OK in implementation files or scopes.

hageboeck commented 1 year ago

Honestly I would not mention this at all, or if I would it would be to strongly recommend to NEVER use using namespace std. To me it's certainly not "OK in implementation files or inside scopes". You get the same clashes and vicious bugs as anywhere else.

As a correlate we should also strongly recommend to not use at all using namespace. Such a need is a clear indication of a flow in the hierarchy of namespaces put in place. If you're in the right namespace, you won't need it.

I would say that there is an exception to "you don't need it": https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c165-use-using-for-customization-points

I agree that ADL + using is a very advanced topic, so we should go simpler here and first explain what using does and then that it should be used only very sparingly ... Is it OK if I remove the statement that "It is OK", and instead we warn to not overdo it?

hageboeck commented 1 year ago

@sponce @bernhardmgruber I think now it's cautioning people to not overly used it. This slide hopefully gives enough material to allow for a good explanation during the lecture. OK to merge?

sponce commented 1 year ago

Kind of ok for me now. Although I would name it "using namespace directive" and be slightly more strict, saying : "Avoid as much as you can" and adding "Prefer raw using"

Note that I disagree with your point stating "there is an exception". I really believe using namespace should never be used. Now using can of course, no problem there for a targeted import you know won't clash. You'll notice by the way that in your pointer https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c165-use-using-for-customization-points that's what they use, not using namespace

hageboeck commented 1 year ago

Note that I disagree with your point stating "there is an exception". I really believe using namespace should never be used. Now using can of course, no problem there for a targeted import you know won't clash. You'll notice by the way that in your pointer https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c165-use-using-for-customization-points that's what they use, not using namespace

We don't disagree. I had just misunderstood your comment. 🙂 Since we all don't use using directives (using namespace), but use using declarations, I'll do one final rephrasing. The slide title is already changed as you suggested.

sponce commented 1 year ago

I must say that I'm a bit lost in the vocabulary here : directive means namespace and declaration means no namespace, that's it ? Then maybe make it clear, e.g. by adding to the title '(aka using namspace)' for example.

bernhardmgruber commented 1 year ago

I must say that I'm a bit lost in the vocabulary here : directive means namespace and declaration means no namespace, that's it ?

Exactly. See also: https://en.cppreference.com/w/cpp/keyword/using

sponce commented 1 year ago

I like the last version ! Maybe just a detail if it fits on the slide, I would split into 2 lines the advice "Qualify names (\cppinline{std::vector}), put things that belong together in the same namespace", that is :