Why is it not a good practice to use "std" namespace in C++?


I was using using namespace (nomeDaBiblioteca); in my code and I ended up having some conflicts with another library.

Why do these conflicts happen and what is the best solution?


It's good practice. When it's useful and when you know how to use it, knowing it has some implications.

You may have encountered a problem for a specific situation.

the real problem

Perhaps the bad practice has already started in the library that used the same names as the standard language library.

But I don't even say that this is really a problem, after all namespaces were created for this very thing, to disambiguate the same names of different things.

What's a bigger problem is putting the using inside a header because then you include the using without seeing that it's there. This problem can be much bigger. The problem is to use it globally. The more location the better (see Lucas Nunes' answer), using it consciously doesn't cause any problems.

The solution

The answer is correct in giving the solution the full method name including the namespace .

I would say it's a matter of taste for most cases whether to always use the full name or not. Most programmers choose not to use the full name but use using . Only when there is a conflict that should require a full name. I don't say what each should do I say just create a rule and keep it consistent throughout the project, or adopt the existing rule from an ongoing project.

An interesting rule of thumb in these cases would be to use the full name only in extra libraries and not std , or the opposite when std is used very little.

If there really is a need to load multiple namespaces that might be conflicting, there's probably something wrong with this code.

But if you're afraid of possible conflicts, of course, the solution is to be more cautious than it really should be. I just don't think it's a solution for everyone.

There is an example showing a possible solution to conflicts:

namespace My_lib {

    using namespace His_lib; // everything from His_lib
    using namespace Her_lib; // everything from Her_lib

    using His_lib::String; // resolve potential clash in favor of His_lib
    using Her_lib::Vector; // resolve potential clash in favor of Her_lib


I put it on GitHub for future reference .

Source: The C++ Programming Language


In the linked OS question itself there are solutions to avoid major problems. And talk about the points I talked about here.

The other question linked here shows the use in C# which is extremely encouraged. Of course in C# things are a little more organized than in C++ but it doesn't change that much.

I'm not preaching here that everyone skips using fully qualified names but I almost always use using and I've never had any problems. Of course, I choose the additional libraries I use well and analyze if they can get me into trouble.

I don't see most programmers worried about this and their code often uses using . There are many official recommendations of what not to use in the language and this is not one of them. The code is more readable this way.

bad practice

The problem is trying to define as a bad practice what is actually a point-in-time inconvenience that can be circumvented when you understand the whole workings of what you are using.

Scroll to Top