What is using namespace std in c
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. And as for making Boost's regex part of std. I do that for future integration and — again, I admit fully this is bias - I don't think it is as ugly as boost::regex Indeed, that is another thing for me. Even those that I do accept it was difficult, and I still have issues with them.
From my experiences, if you have multiple libraries that uses say, cout , but for a different purpose you may use the wrong cout. For example, if I type in, using namespace std; and using namespace otherlib; and type just cout which happens to be in both , rather than std::cout or 'otherlib::cout' , you might use the wrong one, and get errors. It's much more effective and efficient to use std::cout. With unqualified imported identifiers you need external search tools like grep to find out where identifiers are declared.
This makes reasoning about program correctness harder. It depends on where it is located. If it is a common header, then you are diminishing the value of the namespace by merging it into the global namespace.
Keep in mind, this could be a neat way of making module globals. This is a bad practice, often known as global namespace pollution. Problems may occur when more than one namespace has the same function name with signature, then it will be ambiguous for the compiler to decide which one to call and this all can be avoided when you are specifying the namespace with your function call like std::cout.
Hope this helps. I put it the other way around: Why is typing five extra characters considered cumbersome by some? Consider e. Why would I even consider polluting my global namespace by cutting general "std::vector" down to "vector" when "vector" is one of the problem domain's most important concepts?
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Why is "using namespace std;" considered bad practice? For example:. This error can occur when namespace members are declared across multiple header files, and you have not included those headers in the correct order. If an identifier is not declared in an explicit namespace, it is part of the implicit global namespace. In general, try to avoid making declarations at global scope when possible, except for the entry point main Function , which is required to be in the global namespace.
To explicitly qualify a global identifier, use the scope resolution operator with no name, as in ::SomeFunction x ;. This will differentiate the identifier from anything with the same name in any other namespace, and it will also help to make your code easier for others to understand. Namespaces may be nested. An ordinary nested namespace has unqualified access to its parent's members, but the parent members do not have unqualified access to the nested namespace unless it is declared as inline , as shown in the following example:.
Ordinary nested namespaces can be used to encapsulate internal implementation details that are not part of the public interface of the parent namespace. In contrast to an ordinary nested namespace, members of an inline namespace are treated as members of the parent namespace. This characteristic enables argument dependent lookup on overloaded functions to work on functions that have overloads in a parent and a nested inline namespace.
It also enables you to declare a specialization in a parent namespace for a template that is declared in the inline namespace. The following example shows how external code binds to the inline namespace by default:.
The following example shows how you can declare a specialization in a parent of a template that is declared in an inline namespace:.
You can use inline namespaces as a versioning mechanism to manage changes to the public interface of a library. For example, you can create a single parent namespace, and encapsulate each version of the interface in its own namespace nested inside the parent. The namespace that holds the most recent or preferred version is qualified as inline, and is therefore exposed as if it were a direct member of the parent namespace. Client code that invokes the Parent::Class will automatically bind to the new code.
Clients that prefer to use the older version can still access it by using the fully qualified path to the nested namespace that has that code. The inline keyword must be applied to the first declaration of the namespace in a compilation unit. Therefore, computer needs to know what kind of data we store so that computer reserves some memory space for the variable. A Byte [B] is the minimum amount of memory space for a computer.
One Byte is eight bits and a bit is either 0 or 1. It is also the number of bytes taken by a data type is compiler dependent. Following program has int a, b, and c. Prints a, b, and c. Variable is a location in memory where a value can be stored. Declare variables with data type and name before use.
0コメント