Namespace (using namespace std;)

Very often on the Internet I see how many programmers diligently write programs everywhere using std:: in the code. Why are they doing this? Why can't you just use using namespace std; before the program, it's also more convenient and the code starts to "breathe". Or is this a bad tone and it is worth retraining to use std:: directly in the program code?

Author: Abyx, 2015-04-11

5 answers

Depends on the traditions. Among the pluses is the tradition "it is better to be safe than to get sudden hard-to-debug problems somewhere".

Explicitly specifying a namespace is a way to avoid potential problems in the future. Let's say you connected two namespaces via using namespace. Everything is wonderful, brief, beautiful.

And then a new version of one of the libraries was released, and some identifiers began to resolve in a different way, for example, in the second library they added a function that fits your arguments better than the function you used earlier from the first library.

At best, your code won't build. It may fall. Or it may happen that your code will stop working for the client in 1% of cases. Anything can happen.

Catching and fixing such problems is excruciatingly painful.

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 even just the standard one library), then you can not 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 banal example: let's say you use only the standard library and boost, so you decided to write everywhere:

using namespace std;
using namespace boost;

...and now there is a new version of the standard library, in which many classes are dragged from boost. And suddenly your code is no longer there compiles.

Other languages have different traditions. For example, C# almost always writes short class names, and only explicitly specifies the namespace or uses aliases in case of conflicts. The language is slightly different: there are no functions outside of classes. This allows you to lose less readability and run into unexpected conflicts less often.

 36
Author: Kyubey, 2018-08-03 10:53:36

In large projects that use many third-party libraries, it often makes sense to specify a namespace before the construct used. Especially when their names start to overlap. Well, just an instant understanding of where and what comes from is a big time saver, rather than the complexity of reading a three-letter word. " std " I mean :)

 9
Author: alexis031182, 2015-04-11 14:40:21

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, introduced into the code by the header file, can break everything.

However, in cpp files, I use using namespace std all the time. And I sleep quite calmly. It never causes any unexpected problems.

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

 7
Author: Shamov, 2015-04-17 15:21:08

Here's what the Google Style Guide says about this:

// Запрещено - Это загрязняет пространство имен.
using namespace foo;

You can use the using-declaration anywhere in the file .cc, 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.

 4
Author: zenden2k, 2017-12-15 10:12:42

"Clogging up" the current namespace is bad form, use only what you need. For example, so:

using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::swap;
и т.д.
 3
Author: 0x1337, 2015-04-17 14:56:31