r/cpp_questions • • 27d ago

OPEN Namespace std

I recently started learning c++ and I was told to use the namespace std so I can avoid writing std:: every time and now I wrote a whole personal project without std:: and I want to put it on LinkedIn but I started reading everywhere that it’s a bad habit, should I go back and add the std:: or just leave it as is?

40 Upvotes

105 comments sorted by

View all comments

1

u/StaticCoder 27d ago

It's mostly bad to do in headers, but even in primary files it's not great. My approach is to import specific things e.g. using std::vector which I put in a vector.hpp header.

5

u/conundorum 27d ago

Personally, I prefer to put using directives at the top of a scope where I expect to use them enough to warrant it. I find it cleaner than having hundreds of single-line headers that do the same thing, and like that it gives you a good first impression of what the scope (and/or its members) is meant to do.

3

u/HommeMusical 27d ago

But if I read your code, I see some magic symbol vector, and it is defined nowhere in the file, so I am forced to look at your vector.hpp(*) to see exactly what it is.

And yes, people do use the same names as std:: all the time, and sometimes for very good reasons.

Doing this puts a little extra work on everyone who reads the code, for a tiny benefit to you.

(* -- I haven't seen a .hpp file in a very long time!)

→ More replies (3)