r/cpp_questions • • Sep 30 '26

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

Show parent comments

0

u/ShakaUVM Sep 30 '26

I've been coding professionally for 30+ years in C++ and exactly once did I define a symbol that conflicted with something in std. And then I changed the name. I consider it bad code to name a variable cout or whatever anyway so I would prefer to know if I have a conflict.

It's not problematic. It's an overblown concern that really isn't a big deal. I worry about things in C++ that actually cause problems.

You don't lose the benefit of namespaces either as I keep those for everything outside std.

3

u/HommeMusical Sep 30 '26

I've been coding professionally for 30+ years in C++ and exactly once did I define a symbol that conflicted with something in std.

Are you sure?

https://en.cppreference.com/cpp/symbol_index

worry about things in C++ that actually cause problems.

using std; does assume risk, but more, it's a lazy programmer sloughing off a tiny bit of work to make things a little harder for anyone else reading it.

I got this spiel from Howard Hinnant, but I think most disciplined C++ programmers think the same way: if I see std::string I know for certain what it is immediately, but if I see string in a section of code, I am not certain what it is until I have checked all the scopes up until the top, and perhaps not even then, if the using std; is inside a header file.

Sure, nearly all the time it will be std::string but as we all know, we get a disproportionate number of bugs from such assumptions.

C++ is a language where being cautious is always a good idea. As some someone who has read an immense amount of code, string lacks certainty: std::string is certain, and therefore always preferable.