20
I was using using namespace (nomeDaBiblioteca);
in my code and ended up having some conflicts with another library.
Why these conflicts happen and what is the best solution?
20
I was using using namespace (nomeDaBiblioteca);
in my code and ended up having some conflicts with another library.
Why these conflicts happen and what is the best solution?
18
It’s good practice. When it’s useful and when you know how to use, knowing it has some implications.
You may have encountered a problem by a specific situation.
Maybe bad practice has already started in the library that used names like the standard language library.
But I’m not even saying this is a real problem, after all namespaces were created for this very thing, to disambiguate equal names of different things.
What’s a bigger problem is putting the using
within a header because then you include the using
without seeing that it is there. This problem can be much bigger. The problem is to use globally. The more local the better (see Lucas Nunes' answer), using consciously it does not cause problems.
The answer is correct in giving the solution of the full name of the method including the namespace.
I would say that it is a matter of taste for most cases whether you should always use the full name or not. Most programmers choose not to use the full name but to do the using
. Only when there’s a conflict that should require a full name. I do not say what each should do I say just to create a rule and keep it consistent throughout the project, or adopt the existing rule of a project in progress.
An interesting rule in these cases would be to use the full name only in the extra libraries and not in the std
, or the opposite when the std
is used very little.
If there really is need to carry several namespaces which may be conflicting, there’s probably something wrong with this code.
But if you’re afraid of possible conflicts, of course, the solution is to take more precautions than should really be necessary. I just don’t think it’s 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 in the Github for future reference.
Source: The C++ Programming Language
In the OS’s own question linked has solutions to avoid major problems. And talks about the points I mentioned here.
On the other question from here linked 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 stops using fully qualified names but I almost always use the using
and never had any problems. Obviously, I choose well the additional libraries I use and analyze if it can bring me problems.
I don’t see most programmers concerned with this, and their code usually uses using
. There are many official recommendations of what is not to use in the language and this is not one of them. The code becomes more readable this way.
The problem is trying to define as bad practice what is actually an inconvenient punctual measurable when understanding the whole functioning of what you are using.
9
A less "aggressive" way of using the using namespace
is to do within a scope.
This way you have the same ease, having more control over the namespace.
For example, you can use the 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 rotate on ideone.
Of course the rules are the same for this case. Make a:
using namespace foo;
using namespace bar;
Within a certain scope will have the same consequences.
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, follows below example:
We have two libraries
using namespace foo;
using namespace bar;
If there are methods in the two libraries whose name is the same it will give conflict, then it is better to use foo::metodo() e bar::metodo()
Thus, even containing the same method name we are making explicit 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’s a topic here at SOPT explaining how namespaces work: How namespaces work in C#?
And the reference I used was the SOEN: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
Browser other questions tagged c++ namespace
You are not signed in. Login or sign up in order to post.
It’s bad practice to use
using
inside a header. Otherwise I agree not to see any problem. And in case of conflict, just treat the conflicted name especially using the most complete notation, example:std::string
andfoobar::string
. The appealusing
often makes the code more readable and should rather be used. + 1.– Guilherme Bernal