Home c++ Namespace (using namespace std;)

Namespace (using namespace std;)

Author

Date

Category

Very often on the Internet I see how many programmers diligently write programs everywhere using std :: in their code. Why are they doing that? Why can’t you just use using namespace std; in front of the program, it’s also more convenient and the code starts to “breathe”. Or is it bad form and is it worth relearning to use std :: directly in the program code?


Answer 1, authority 100%

Depends on tradition. Among the pluses is the tradition “it’s better to play it safe than to get sudden hard-to-debug problems no one knows where”.

Explicitly specifying a namespace is to get rid of potential future problems. Let’s say you include two namespaces via using namespace . Everything is great, short, beautiful.

And then a new version of one of the libraries came out, and some identifiers began to be resolved differently, for example, in the second library a function was added that suits your arguments better than the function you used earlier from the first library.

At best, your code won’t compile. May fall. Or it may happen that your code will stop working for the client in 1% of cases. Anything can be.

It is excruciatingly painful to catch and fix these kinds of problems.

How important it is for you specifically is up to you. If you have a simple project and at most a couple of third-party libraries (or generally only the standard library), then you don’t have to bother with explicitly specifying namespaces. If the project is huge, with dozens of libraries, it may be more convenient (and intuitive) to always specify namespaces.

A trivial example: let’s say you only use the standard library and boost, so you decided to write everywhere:

using namespace std;
using namespace boost;

… and now a new version of the standard library is coming out, in which a lot of classes have been dragged from boost. And suddenly your code doesn’t compile anymore.

Other languages ​​have different traditions. For example, in C #, short names of classes are almost always written, and only in case of conflicts they explicitly indicate the namespace or use aliases. The language is a little different: there are no functions outside of classes. This means less readability and less unexpected conflicts.


Answer 2, authority 24%

In large projects that use many third-party libraries, it often makes sense to include the namespace before the construct being used. Especially when their names start to intersect. Well, just an instant understanding of where and what comes from is a big time-saver than the difficulty of reading a three-letter word. “std” I mean 🙂


Answer 3, authority 19%

In header files, you should always specify the namespace explicitly, because you never know exactly where this file will be included later. An unexpected using namespace std added to the code by the header file can break everything.

However, in cpp files I use using namespace std all the time. And at the same time I sleep perfectly well. It never causes any unexpected problems.

Moreover, even in header files, you can sometimes use using namespace . For example, inside the body of a templated function. The main thing is to make sure that the effect of combining the namespace does not leak out of the area where it brings tangible and visible benefits to the eye.


Answer 4, authority 11%

Here’s what Google Style Guide says about it:

// Disallowed - This pollutes the namespace.
using namespace foo;

You can use the using declaration anywhere in the .cc file, and in functions, methods and classes in header files.

// OK in .cc files.
// Must be in a function, method or class in .h files.
using :: foo :: bar;

I would not distrust the recommendations of these guys, they are writing a huge Chromium project.


Answer 5, authority 8%

It is bad practice to “litter” the current namespace, only use what you need. For example, like this:

using std :: string;
using std :: cin;
using std :: cout;
using std :: endl;
using std :: swap;
etc.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions