使用命名空间..很糟糕吗? [英] Is using namespace..like bad?

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

问题描述

可能重复:
为什么'使用命名空间std;'在 C++ 中被认为是一种不好的做法?

每次我使用 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.

有人愿意解释吗?在我看来,它只是让输入 coutstd::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.libFoo 的 2.1 版问世,突然你的程序不再编译.您的 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 中包含他们以前没有的.现在您的源文件中包含了所有标准算法,并且您的 using namespace 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.

相关讨论的主题在这里:

Thread with relevant discussion is here:

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

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

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