Writing clean, unambiguous and robust error-free code should be the intent of any programming developer. Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. Like Article. Difficulty Level : Medium. Recommended Articles. Why overriding both the global new operator and the class-specific operator is not ambiguous? Article Contributed By :. Sayan Mahapatra. Easy Normal Medium Hard Expert.
I also consider it a bad practice. Just one day I thought that the function of a namespace is to divide stuff, so I shouldn't spoil it with throwing everything into one global bag.
However, if I often use 'cout' and 'cin', I write: using std::cout; using std::cin; in the. I think that no one sane will ever name a stream cout or cin.
It's nice to see code and know what it does. If I see std::cout I know that's the cout stream of the std library. If I see cout then I don't know. It could be the cout stream of the std library. Or a static variable named cout in that file.
It could be anything. Now take a million line code base, which isn't particularly big, and you're searching for a bug, which means you know there is one line in this one million lines that doesn't do what it is supposed to do.
Looking for a bug, I'd have to check that. Can you see how I really really prefer to see std::cout? It's one of these things that seem a really good idea if you are a teacher and never had to write and maintain any code for a living.
I love seeing code where 1 I know what it does; and, 2 I'm confident that the person writing it knew what it does. It's all about managing complexity. Using the namespace will pull things in that you don't want, and thus possibly make it harder to debug I say possibly. Using std:: all over the place is harder to read more text and all that. Note that this is a simple example. If you have files with 20 includes and other imports, you'll have a ton of dependencies to go through to figure out the problem.
The worse thing about it is that you can get unrelated errors in other modules depending on the definitions that conflict. It's not horrible, but you'll save yourself headaches by not using it in header files or the global namespace. It's probably all right to do it in very limited scopes, but I've never had a problem typing the extra five characters to clarify where my functions are coming from.
A concrete example to clarify the concern. Imagine you have a situation where you have two libraries, foo and bar , each with their own namespace:.
Now let's say you use foo and bar together in your own program as follows:. At this point everything is fine. When you run your program it 'Does something'. But later you update bar and let's say it has changed to be like:. So you'll need to do some maintenance to clarify that 'a' meant foo::a.
That's undesirable, but fortunately it is pretty easy just add foo:: in front of all calls to a that the compiler marks as ambiguous. At this point your call to a 42 suddenly binds to bar::a instead of foo::a and instead of doing 'something' it does 'something completely different'. No compiler warning or anything. Your program just silently starts doing something completely different than before.
When you use a namespace you're risking a scenario like this, which is why people are uncomfortable using namespaces. The more things in a namespace, the greater the risk of conflict, so people might be even more uncomfortable using namespace std due to the number of things in that namespace than other namespaces. Ultimately this is a trade-off between writability vs. Readability may factor in also, but I could see arguments for that going either way.
The 'best' trade-off will determine on your project and your priorities. Using many namespaces at the same time is obviously a recipe for disaster, but using JUST namespace std and only namespace std is not that big of a deal in my opinion because redefinition can only occur by your own code People should stop being so anal about it.
Your teacher was right all along. Just use ONE namespace; that is the whole point of using namespaces the first place. You are not supposed to use more than one at the same time. Unless it is your own. So again, redefinition will not happen. You need to be able to read code written by people who have different style and best practices opinions than you.
If you're only using cout , nobody gets confused. But when you have lots of namespaces flying around and you see this class and you aren't exactly sure what it does, having the namespace explicit acts as a comment of sorts. You can see at first glance, "oh, this is a filesystem operation" or "that's doing network stuff". I agree with the others here, but I would like to address the concerns regarding readability - you can avoid all of that by simply using typedefs at the top of your file, function or class declaration.
I usually use it in my class declaration as methods in a class tend to deal with similar data types the members and a typedef is an opportunity to assign a name that is meaningful in the context of the class. This actually aids readability in the definitions of the class methods. A namespace is a named scope. Namespaces are used to group related declarations and to keep separate items separate. For example, two separately developed libraries may use the same name to refer to different items, but a user can still use both:.
Repeating a namespace name can be a distraction for both readers and writers. Consequently, it is possible to state that names from a particular namespace are available without explicit qualification.
For example:. Namespaces provide a powerful tool for the management of different libraries and of different versions of code. In particular, they offer the programmer alternatives of how explicit to make a reference to a nonlocal name.
An example where using namespace std throws a compilation error because of the ambiguity of count, which is also a function in algorithm library. It doesn't make your software or project performance worse. The inclusion of the namespace at the beginning of your source code isn't bad. The inclusion of the using namespace std instruction varies according to your needs and the way you are developing the software or project.
As is mentioned in this page :. The statement using namespace std is generally considered bad practice. The alternative to this statement is to specify the namespace to which the identifier belongs using the scope operator :: each time we declare a type. And see this opinion :. There is no problem using "using namespace std" in your source file when you make heavy use of the namespace and know for sure that nothing will collide.
Some people had said that is a bad practice to include the using namespace std in your source files because you're invoking from that namespace all the functions and variables. When you would like to define a new function with the same name as another function contained in the namespace std you would overload the function and it could produce problems due to compile or execute. It will not compile or executing as you expect.
Although the statement saves us from typing std:: whenever we wish to access a class or type defined in the std namespace, it imports the entirety of the std namespace into the current namespace of the program. Let us take a few examples to understand why this might not be such a good thing. Notice how there is an ambiguity, to which library does cout point to?
The compiler may detect this and not compile the program. In the worst case, the program may still compile but call the wrong function, since we never specified to which namespace the identifier belonged. It's case by case. We want to minimize the "total cost of ownership" of the software over its lifespan. Stating "using namespace std" has some costs, but not using it also has a cost in legibility. People correctly point out that when using it, when the standard library introduces new symbols and definitions, your code ceases to compile, and you may be forced to rename variables.
And yet this is probably good long-term, since future maintainers will be momentarily confused or distracted if you're using a keyword for some surprising purpose. You don't want to have a template called vector, say, which isn't the vector known by everyone else.
There is a cost to having to do this kind of change, but the cost is not high and is offset by the clarity gained by not using std symbol names for other purposes. An algorithm or step in a method that could be taken in on one screenful of code now requires scrolling back and forth to follow.
This is a real cost. Arguably it may not be a high cost, but people who deny it even exists are inexperienced, dogmatic, or simply wrong. It is the one library everyone basically needs to know, and in my view is best thought of as part of the language. Generally speaking there is an excellent case for using namespace std even if there isn't for other libraries.
Never force the decision onto the author of a compilation unit a. Always defer the decision to the compilation unit author.
Even in a project that has decided to use using namespace std everywhere may fine a few modules that are best handled as exceptions to that rule. Even though the namespace feature lets you have many modules with symbols defined the same, it's going to be confusing to do so.
Keep the names different to the extent possible. Even if not using the namespace feature, if you have a class named foo and std introduces a class named foo , it's probably better long-run to rename your class anyway.
An alternative to using namespaces is to manually namespace symbols by prefixing them. I have two libraries I've used for decades, both starting as C libraries, actually, where every symbol is prefixed with "AK" or "SCWin". Generally speaking, this is like avoiding the "using" construct, but you don't write the twin colons.
AK::foo is instead AKFoo. ASCII 0 and 1 in the case of the database! A nice benefit of this is that it gives an organic slope from being part of a project to eventually being a library.
In a large application of mine, all window classes are prefixed Win , all signal-processing modules Mod, and so on. There's little chance of any of these being reused so there's no practical benefit to making each group into a library, but it makes obvious in a few seconds how the project breaks into sub-projects.
I do not think it is necessarily bad practice under all conditions, but you need to be careful when you use it. If you're writing a library, you probably should use the scope resolution operators with the namespace to keep your library from butting heads with other libraries.
For application level code, I don't see anything wrong with it. I agree with others — it is asking for name clashes, ambiguities and then the fact is it is less explicit. While I can see the use of using , my personal preference is to limit it. I would also strongly consider what some others pointed out:.
If you want to find a function name that might be a fairly common name, but you only want to find it in the std namespace or the reverse — you want to change all calls that are not in namespace std , namespace X , You could write a program to do it, but wouldn't it be better to spend time working on your project itself rather than writing a program to maintain your project?
Personally, I actually don't mind the std:: prefix. I like the look more than not having it. I don't know if that is because it is explicit and says to me "this isn't my code I am using the standard library" or if it is something else, but I think it looks nicer.
There is one other thing although it is somewhat related to the above and what others point out. While this might be bad practise, I sometimes reserve std::name for the standard library version and name for program-specific implementation.
Yes, indeed this could bite you and bite you hard, but it all comes down to that I started this project from scratch, and I'm the only programmer for it. Example: I overload std::string and call it string. I have helpful additions. Besides that, you can have namespace aliases. Here is an example of where it is useful that might not have been referred to.
Well, it doesn't have complete std::regex support. Sure, it compiles, but it throws an exception along the lines of it being an error on the programmer's end. But it is lack of implementation. So here's how I solved it. Install Boost's regex, and link it in. I won't argue on whether that is a bad idea or not.
Yes, starting your own project and starting with a standard The string is the exception ignore the first, above, or second here, pun if you must for me as I didn't like the idea of 'String'. Sparing details, much of what I work on fits C more but it was a good exercise and a good way to make myself a. But what is useful is what some already suggested: I do indeed use list it is fairly generic, is it not?
Put simply: no assuming allowed. So they created a namespace, std to contain this change. The using namespace statement just means that in the scope it is present, make all the things under the std namespace available without having to prefix std:: before each of them. While this practice is okay for short example code or trivial programs, pulling in the entire std namespace into the global namespace is not a good habit as it defeats the purpose of namespaces and can lead to name collisions.
Even if there are initially no name collisions, they can crop up during maintenance as more code, libraries, etc.
0コメント