是使用命名空间。 [英] Is using namespace..like bad?

查看:194
本文介绍了是使用命名空间。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:





每次使用 using namespace std 我总是得到这是一个可怕的编程习惯。
现在我在12月份毕业了。在C.S.但我不声称知道一切,但没有人曾经解释过为什么这么糟。我明白它的作用,但我真的没有看到一个巨大的交易。

Every time I use using namespace std I always get that "thats a terrible programming habit". Now I'm graduating this December with my B.S. in C.S. but I don't claim to know everything, but no one has ever explained why this is so bad. I understand what it does but I honestly don't see a huge deal with it.

任何人关心解释?在我看来,它只是使输入 cout std :: cout 更容易。

Anyone care to explain? In my mind it just makes typing cout a whole lot more bearable than std::cout.

我可以理解为什么你不想把它放在一个头文件,但只是在一个正常的实现文件...我不明白为什么会

I can understand why you wouldn't want to put it in a header file, but just in a normal implementation file... I dont see why it would be a problem.

推荐答案

在其他地方发现这个有用的帖子:

found this useful post elsewhere:

命名空间分离和组织功能。你可以有一个 xander333 :: sort()函数,它不会与 std :: sort() boost :: sort()或任何其他sort()。没有命名空间只能有一个 sort()

Namespaces separate and organize functionality. You can have a xander333::sort() function and it won't conflict with std::sort() or boost::sort() or any other sort(). Without namespaces there can be only one sort().

现在我们假设你输入了using namespace std ;在所有源文件中,并且已经在您的一个文件的全局命名空间中实现了一个简单的模板函数 fill()。此文件还取决于来自libFoo - foo.hpp 的标头。 2.1版本的libFoo出来,突然你的程序不再编译。您的版本 fill()突然与另一个 fill()冲突!发生了什么?

Now let's say you've put "using namespace std;" in all your source files and you've implemented a simple templated function called fill() in the global namespace of one of your files. This file also depends on a header from libFoo -- foo.hpp. Version 2.1 of libFoo comes out and all of a sudden your program no longer compiles. You version of fill() suddenly conflicts with another fill()! What happened?

事实证明,实现libFoo的人包括在新版本的 foo.hpp 以前没有。现在你有所有的标准算法被包含在你的源文件中,并且你的使用命名空间std; 已经把它们全部放入全局命名空间。 std :: fill()现在直接与您的 fill()冲突。

It turns out that the folks implementing libFoo included in the new version of foo.hpp when they didn't before. Now you have all of the standard algorithms being included in your source file, and your using namespace std; has pulled them all into the global namespace. std::fill() now directly conflicts with your fill().

更隐蔽的是,您已经通过将 fill()重命名为 xander333_fill(),但有些东西不能正常工作 - 您的报告数字已关闭。原来,你的自定义 divides()函数,它的固定精度数学,不再被调用,因为模板函数从(也新包括 foo.hpp )可以更好地匹配,因为你调用的类型与声明的类型不完全匹配。

More insidious, you've gotten your code to compile by renaming your fill() to xander333_fill(), but something's not working right -- your report numbers are off. It turns out that your custom divides() function, which does fixed precision math, is no longer being called because the templated function from (also newly included by foo.hpp) makes for a better match because you're calling types did not exactly match the declared types.

在这里:

http:// www.cplusplus.com/forum/unices/27805/

这篇关于是使用命名空间。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆