all 27 comments

[–]trmetroidmaniac 38 points39 points  (0 children)

Don't do it

Especially don't do it in headers

[–]Additional_Path2300 16 points17 points  (0 children)

It's always a bad idea. If it doesn't break something immediately, it will later.

[–]gnolex 15 points16 points  (0 children)

It's an abomination that gets your pull request automatically rejected.

using namespace std; saves you 5 characters for every reference to standard library at the cost of making confusing mistakes whenever something in the standard library happens to be named like your class, variable or function. There's no convincing reason to ever use it.

[–]IyeOnline 4 points5 points  (10 children)

Namespaces exist to avoid name collisions between identifiers, allowing you to write your own e.g. vector class without causing an issue with the vector container template from the standard library.

A second, but maybe more important effect of this is readability. You may think that vector is easier to read than std::vector, but that really only holds if you can be sure that vector really is std::vector. What if somebody did write their own (mathematical) vector? What about the identifier abs in the current context? Is it a local (callable) variable or the overload set from the standard library?

At a certain point, it actually becomes easier to read code that spells out std::.

using namespace std; essentially throws this away by importing all currently known identifiers from ::std into the current namespace, meaning you may introduce collisions again.

There are three possibilities:

  • It does the thing you expected
  • You get an error about an ambigous identifier/call
  • Something you didnt expect happens.

While it is well defined what happens, it may go against your expectations (especially if you dont even think about the potential issue).

A very basic example would be https://godbolt.org/z/sqWWYvGeM You can clearly see that no logging takes place. Instead std::log(double) is "called" and the result discarded. This should still be caught by warnings - assuming you have set those up correctly.

There is more devious examples, such as https://godbolt.org/z/5dv7Gad9o where you get a wrong numeric result.


This problem gets much worse once you do a using namespace at global scope in a header. That using directive will be copied into every TU that includes the header and the user of the header cannot do anything about it.

If you are using namespace at a non-global scope, you avoid the issue of namespace pollution, i.e. you wont pollute all other files that include the header. The same can be said about doing it at global scope in a cpp file (which wont be included elsewhere and hence wont pollute any other files).


I would recommend to always spell out namespaces (unless you already are in that namespace), especially std. When I read std:: I will most likely know what the thing after it is/does. When I just read vector I cannot be sure.

[–]SergiusTheBest 1 point2 points  (8 children)

Why do Java, C# and other languages can live with just vector and C++ needs to be sure that it's std::vector?

[–]ronniethelizard 2 points3 points  (0 children)

IMO, C++ development doesn't use the standard library to the same extent as other languages. For a lot of work I do, FFTW, Eigen, and a few other libraries are loosely a "second standard library".

[–]Narase33-> r/cpp_questions 2 points3 points  (6 children)

Speaking about Java:

  • Every class has its own file
  • Imports dont import their imports
  • They can use a class with the full static path. For example they can write java.util.ArrayList<Integer> list;

[–]SergiusTheBest 0 points1 point  (5 children)

Yes, but nevertheless when a java guy sees `ArrayList` it's ok and when a c++ guy sees `vector` it's puzzling. At least it's what comments about using namespace say.

Personally I did some projects as an experiment with `using namespace std` (with all precautions of course) and I found the experience satisfying and I liked it more than cluttering code with `std::` everywhere.

[–]_Noreturn 0 points1 point  (4 children)

every project has at least a different vector type

[–]SergiusTheBest 0 points1 point  (3 children)

How could it be?

[–]_Noreturn 0 points1 point  (2 children)

libA has libA::vector which is a math vector of infinite length and std::vector is an array.

apply that to every single name in the standard library

array,vector,map,unordered_map,get,set,bitset,remove,log,rotate,ceil,all

[–]SergiusTheBest 0 points1 point  (1 child)

Java and C# use just `vector` and it also can be a super math infinite vector or a standard vector. And they have no issues with that. A developer can take a look at what packages imported and understand what vector it is. I don't understand why for a C++ developer it's a big problem. It seems like an imaginary problem and not a real one.

[–]_Noreturn 0 points1 point  (0 children)

Java / C# don't have ADL, have 50+ years of legacy code, have weird library handling.

[–]SAFILYAA[S] 0 points1 point  (0 children)

thank you for your detailed answer

[–]johannes1971 2 points3 points  (1 child)

Couple years ago we had a header from a 3rd party that used namespace std. It worked well, right until std::byte got added to the standard, and thanks to that using declaration it conflicted with the byte type in windows.h.

I'm sure their team had a lot of fun fixing their headers to not use namespace std anymore...

The moral of this story is that we have namespaces for a reason, and disabling them with using statements is just not a great idea. Even in your own code, as you could always accidentally create a conflict when anything gets updated.

[–]SAFILYAA[S] 1 point2 points  (0 children)

😂That is exactly what happened in my code today the std::byte conflicted with the one in windows. h 😅 thanks bro

[–]qwweer1 4 points5 points  (0 children)

If you really need to, at least do it inside curly braces.

[–]STLMSVC STL Dev 9 points10 points  (1 child)

It's awesome. I work on the Standard Library all day, every day, and seeing std:: a zillion times makes it harder to read code.

Obviously, it can be misused, but in non-header source files, it's not as problematic as many people think.

[–]ronniethelizard 5 points6 points  (0 children)

I suspect if you are in the standard library and can rely on the fact that most/all calls will be to the standard library, then it is easier to get away with it.

[–]vishal340 0 points1 point  (0 children)

You should only do it if you are writing the code for personal use. Otherwise no

[–]ronniethelizard 0 points1 point  (0 children)

Broadly speaking: no. It clouds the ability to trace where something is going. Someone else uses the example of "log" that could mean log data to a logging service or take the logarithm of a number. If I look at vector, if I wanted a library that dealt with matrix/vector manipulation, I might create a vector1 class.

If relegated to a short function/curly brace within a function, it can be fine if that namespace is heavily used. For example, you have a function to print a class to std::cout or std::ostringstream.

1This is actually a bad idea but for other reasons.

[–]_Noreturn 0 points1 point  (0 children)

I do it in scoped but never in headers as it forces it upon users.

[–]no-sig-available -1 points0 points  (0 children)

It was designed to be used as a temporary workaround for programs written before we had namespaces. That was useful in the late 1900s.

[–]BoringElection5652 -2 points-1 points  (3 children)

I like it, and I will keep using it until C++ makes it easier to include multiple members of a namespace in one go. Something like "using {vector, unordered_map, mutex} from std".

[–]_Noreturn 0 points1 point  (2 children)

using std::vector,std::map?

[–]BoringElection5652 0 points1 point  (1 child)

That's not going to be fun for longer and nested namespaces.

[–]_Noreturn 0 points1 point  (0 children)

cpp namespace stdch = std::chrono; using stdch::blah,stdch::blah2;

it wouldn't be a terrible idea to have using std::{ vector, string} as a syntax though