Why using namespace std in c




















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 :. Whats the use of using both of them in a simple program like 'Hello world'. My question will sound stupidic to you but I would really appreciate if you explain it. That isn't at all a stupid question. It contains the code that defines cout, cin, endl, etc. If we want to use them, we need them to be in our code, that's why we include it. Now I never use using outside of a function definition and rarely use using namespace at all.

Discussion In short: You can and should use namespace using declarations and directives liberally in your implementation files after include directives and feel good about it. Community Bot 1 1 1 silver badge. I use the using std::vector; form, since that only pulls a single element from the namespace into pseudo-global scope, therefore leading to far less risk of a collision.

Lightness Races in Orbit you are of course entitled to your opinion. Would have been more helpful if there had been some attempt at explanation why you do not agree with advice given in this answer. Especially would be interesting to understand what is the point of namespaces if 'using' them is bad? Also please do no hesitate to note the ": " I appended to my comment!

And that I didn't say namespaces are bad. I can't help but feel that using namespace is evil like goto is evil. Both have valid uses, but times out of they will be used wrong. So, yeah, with using namespace in the source you won't pollute the namespace of other includes, neat. CharonX But won't the code only break if your source file which calls Foo::baz actually include s the header where Bar::baz is declared?

Seems not that likely to happen. It's like if I write using namespace std; in my main. Billy: There is no other way to support calling userlib::cos userlib::superint. Every feature has a use. Zan: Of course there is. The issue though is that any well designed userlib is going to have their sin and cos inside their own namespace as well, so this really doesn't help you. Unless there's a using namespace userlib before this template and that's just as bad as using namespace std -- and the scope there is not limited.

Furthermore, the only function like this I ever see this happen to is swap , and in such cases I would recommend just creating a template specialization of std::swap and avoiding the whole problem. BillyONeal: Your 7-times-upvoted! Briefly, if x has one or more "associated namespaces" e. Instead of using namespace std; , I would prefer using std::sin; using std::cos; using std::exp;.

Do not use it globally It is considered "bad" only when used globally. Because: You clutter the namespace you are programming in. Readers will have difficulty seeing where a particular identifier comes from, when you use many using namespace xyz;.

Whatever is true for other readers of your source code is even more true for the most frequent reader of it: yourself. Come back in a year or two and take a look You may use it locally Go ahead and use it locally almost freely. Are you sure about that? But std::swap automatically chosing your own custom swap, that is absolutely new to me and I don't really believe it. ChristianRau I think so, yes. I read this on SO somewhere.

We can always ask Howard , he should know. I am digging and digging now Even in the swap case, the clearer and thankfully more common idiom is to write using std::swap; rather than using namespace std;. The more specific idiom has fewer side effects and therefore makes the code more maintainable. The final sentence is wrong. But std::swap itself was emphatically not changed to find some other swap and use it. If std::swap gets called, then std::swap gets used. It might be wiser to just type using std::swap locally though, to reduce the local namespace while at the same time creating self-documenting code.

You are rarely ever interested in the whole std namespace, so just out pick out the parts you are interested in. Still, good point. Another reason is surprise. Martin Beckett Martin Beckett Is this a joke? I genuinely can not tell. And if you don't trust the code then why are you using it in the first place?

BrentRittenhouse cout is a bad example because everyone recognizes it. But imagine future in a financial app. Is it a contract to buy or sell something at a specified date? No it isn't. If the code said std::future you would not be so easily confused. BrentRittenhouse maybe a little bad example, there are at least four different libraries that have cout. May be "is it standard library?

Because curricula of education doesn't use those in education. I have to chase away printfs. Or debugs - from Qt. Show 1 more comment.

So the following are OK: Function-level using-directives and using-declarations inside functions' implementations Source-file-level using-declarations inside source files Sometimes source-file-level using-directives.

Alexander Poluektov Alexander Poluektov 7, 25 25 silver badges 30 30 bronze badges. Oleksiy Oleksiy Yelonek Yelonek 1 1 gold badge 4 4 silver badges 9 9 bronze badges.

That's a local using declaration , a very different thing from a using directive. Azeem 7, 4 4 gold badges 20 20 silver badges 34 34 bronze badges. If someone has redefined std::cout to be an integer, then your problem isn't technical, but social -- someone has it in for you. When I see cout I know it's std::cout, always. If I'm wrong, it's problem of person who wrote this code, not me : — Tien Do.

Horses for courses - manage your complexity how you best can and feel able. Preet Sangha Preet Sangha Debugging is unaffected. It depends upon how you define pull things in. In the context above, using it meant that everything in the std:: namespace was considered to be with the scope. Any identifier could come from that namespace, so you have to consider that when reading code. It creates an ambiguity that simply doesn't exist if you refer to something with namespace only where needed.

Anything that reduced cognitive load for the reader eg. Hence my disclaimer at the end. Using "pull things in" in this context gives the wrong impression - it gives the impression that additional namespace'd declarations will be included in the program, regardless of how you meant it.

I agree with what you've said regarding cognitive load. Ron Warholic Ron Warholic 9, 29 29 silver badges 47 47 bronze badges. Kevin Kevin 7, 4 4 gold badges 26 26 silver badges 31 31 bronze badges. The second scenario clinches the deal for me.

No namespaces again. Cannot have such subtle changes in functionality going undetected under the hood. A fix for that problem would be to allow namespace members to be tagged with versions, and have a means by which a using directive could specify that it should bring in members that are tagged with older version numbers, but not those that are tagged with newer ones. If at the time a programmer writes a using directive, the latest version of the library is , the program includes that version number in the using directive, and any functions that get added later get tagged with higher numbers, the code which specifies version would continue to work the same way as it always had.

So just consider them functions as reserved names like "int" or "class" and that is it. Creating collisions isn't that hard - short strings like min , end and less appear in the std:: namespace. But more, now that std:: has thousands of symbols in it, it's useful for the reader to know where a new symbol they might not know comes from.

The std namespace exists because people, either you, your colleagues, or people writing middleware you use, are not always wise about putting functions inside of namespaces. Thus you may import all of std:: and nothing else, while still invoking a collision between, say, std::min and someone else's legacy ::min from before the time when it was in std.

Dustin Getz Dustin Getz Carl Carl Just a minor comment, while typedef is useful I'd consider making a class that represents Lines instead of using typedef. Rohan Singh Rohan Singh 1 1 silver badge 2 2 bronze badges. Very interesting that a this answer that is based on guidance from no other that Bjarne Stroustrup has earned Nithin Nithin 7 7 silver badges 14 14 bronze badges. Usually you'll have more stuff from the std namespaced than from elsewhere, ergo keeping the using namespace directive might save you typing.

This, and the fact that 'this' is implicit in methods, causes so many bugs and problems I can't even count them, even with the right 'count' variable.

As is mentioned in this page : The statement using namespace std is generally considered bad practice. Now the compiler has no way of knowing which version of xyz function you are referring to within your code. A namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. Using namespace, you can define the context in which names are defined.



0コメント

  • 1000 / 1000