1

Hope you're having a nice weekend.

I am going through a C++ course and the training has been good, however, I want to solidify my understanding of several things that the instructor did not really dive into:

  1. Why is it best practice to use std:: in front of cout, endl, vector initializing, etc when we have "using namespace std" before int main()?
  2. Some other threads on stackoverflow mention to never use "using namespace std" before int main(), why is that?

Are these concepts too advanced for beginner programmers or should I learn them as early on as possible in my career?

Thanks and stay safe!

  • 1
    Couple of reasons: 1) If you have a large code, let's say a custom library, it's useful for someone who reads your code to know whether you used std function or your own written somewhere in your library. 2) If you want to name a variable or function as something that exists in the namespace, you get a compilation error. For example, try to name a variable as 'min' or 'max'. Should you avoid using namespace std then? Well, it depends. I still think that if you write not very long code for yourself only, there is no reason to spam std:: before each word. – Maras May 22 '21 at 16:19
  • 1
    Kate Gregory, a frequent speaker at C++ conventions, says it is okay for *beginners* to use `use namespace std;` when they are starting out. With the caveat that they are doing something not recommended, which they'll have to learn the right way later — which is a more advanced topic from a complete beginner viewpoint. – Eljay May 22 '21 at 16:21

0 Answers0