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

16

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?

    
asked by anonymous 28.10.2014 / 19:02

3 answers

16

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 bad practice has already begun in the library that used the same names as the standard language library.

But I do not even say that this is a problem, after all namespaces were created for this. To disambiguate equal names of different things.

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

The solution

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

I'd 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 to do using . Only when there is a conflict should it require a full name. I do not say what each one should do I say just to create a rule and keep it consistent throughout the project. Or adopt the existing rule of an ongoing project.

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

If you really need to load multiple namespaces that can be conflicting, there is probably something wrong with this code.

But if you are afraid of possible conflicts, of course, the solution is to take more precautions than really should be necessary. I just do not think it's the 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 placed GitHub for future reference .

Source: The C ++ Programming Language

Conclusion

In the very question of SO linked has solutions to avoid major problems. And talk about the points I've spoken here.

In the other question here linked shows usage in C # which is greatly encouraged. Of course in C # things are a bit more organized than in C ++ but it does not change that much.

I'm not preaching here that everyone should stop using fully qualified names, but I almost always use using and I've never had problems. Of course, I choose well the additional libraries I use and analyze if it can bring me problems.

I do not see most programmers worried about this, and their code usually uses using . There are many official recommendations of what is not to use in language and this is not one of them. The code becomes more readable this way.

Bad practice

The problem is to try to define as malpractice what is in fact an inconvenient punctually avoidable when you understand the whole operation of what you are using.

    
14.12.2014 / 22:29
9

A less "aggressive" way to use using namespace is to do within a scope. That way you have the same facility, having more control over the namespace.

For example, you can use using namespace std only within a function.

void hello() {
    using namespace std;

    cout << "hello world" << endl;
    // Várias outras utilizações de std.
}

Another example:

#include <iostream>

int main() {

    {
        using namespace std;
        cout << "Aqui não precisa de std." << endl;
    }

    std::cout << "Aqui precisa de std." << std::endl;

    return 0;
}

You can run on ideone .

Of course the rules are the same even for this case. Make one:

using namespace foo;
using namespace bar;

Within a given scope it will have the same consequences.

    
15.12.2014 / 02:09
3

The problem I faced was due to two libraries that contained the same method name and so the use of "using namespace std" is not good practice, here's an example:

We have two libraries

using namespace foo;
using namespace bar;

If there are methods in both libraries whose name is the same, it will conflict, so it is best to use foo::metodo() e bar::metodo()

In this way, even containing the same method name we are explaining which lib they are, so it will not generate conflict.

Another common problem is that std has several identifiers that are common in other libs like:

list, sort, string, iterator

That is, one more item that could easily conflict with your code.

There is a topic here in the SOPT explaining how namespaces work: How do C # namespaces work?

And the reference I used was SOEN: link

    
28.10.2014 / 19:02