使用namespace std进行删除会导致程序获取垃圾结果 [英] Removing using namespace std causes the program to get crap results

查看:153
本文介绍了使用namespace std进行删除会导致程序获取垃圾结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的最小二乘拟合程序,我最近正在调试。我通过将它们调试到控制台进行调试,为了更容易,我使用命名空间std使用

  

和funnily,经过几个星期的编码,我想删除这个,因为调试完成,惊喜是删除它导致结果是错误的!我在g ++和icpc(intel编译器)中进行了一个完整的抽象检查,其中我删除并恢复了该指令,当它被删除时,结果错误...



I知道这是一个非常广泛的问题,不容易得到答案,但在这种情况下你会做什么?你如何调试这样的依赖?

解决方案

这就是为什么在全局命名空间(或任何其他不适当的范围)是一个坏主意。这不仅仅是一种风格的问题,它可以以微妙的方式改变代码的含义。有关问题的详细讨论,请参见此问题



几乎肯定,您的代码不使用标准库中的名称,而是在全局命名空间中声明这些名称。因此,使用命名空间std; 删除会更改这些不合格名称的含义,而不仅仅是使代码无法编译。



这些可能是您的代码中的名称;或者也许是C库函数。许多(特别是< cmath> 中的那些)对于各种类型都是重载的,其中C库本身只声明一个函数。无论是否将某些或全部这些都转储到全局命名空间以及命名空间std 中,这是不明确的。



例如,你可能有一个函数调用

  float angle = whatever; 
float sine = sin(angle);

使用命名空间std; 使用选择您已转储到全局命名空间中的重载 float std :: sin(float)。没有它,根据实现选择与全局命名空间做什么,它可能仍然称之为;或者可以从C库调用 double sin(float);或者它可能无法编译。



在GCC的情况下,似乎C库函数,但不是 C ++重载转载到全球命名空间中,正如我在回答此问题时发现的一样。


I have a complicated least-squares fitting program, which I was recently debugging. I was debugging stuff by cout-ing them to the console, and to make it easier, I used

using namespace std;

and funnily, after weeks of coding, I wanted to remove this because debugging is done, and the surprise was that removing it causes the result to be wrong! I did a complete abstract check in g++ and icpc (intel compiler) where I remove and restore this directive, and when it's removed, the result it wrong...

I know it's a very broad question and not easy to hit the answer, but what would you do in this case? How would you debug such a dependence?

解决方案

This is why using-directives in the global namespace (or any other inappropriately wide scope) are a bad idea. It's not just a matter of style; it can change the meaning of the code in subtle ways. See this question for a thorough discussion of the issues.

Almost certainly, your code uses names from the standard library without qualification, and those names are also declared in the global namespace. Thus, removing using namespace std; changes the meaning of those unqualified names, rather than just making the code fail to compile.

These might be names from your code; or perhaps they are C library functions. Many (particularly those in <cmath>) are overloaded for various types, where the C library itself only declares a single function. It's unspecified whether none, some, or all of these are dumped into the global namespace as well as namespace std.

For example, you might have a function call

float angle = whatever;
float sine = sin(angle);

With using namespace std;, this will select the overloaded float std::sin(float) that you've dumped into the global namespace. Without it, depending on what the implementation chooses to do with the global namespace, it might still call that; or it might call double sin(float) from the C library; or it might fail to compile.

In the case of GCC, it seems that the C library function, but not the C++ overloads, are dumped into the global namespace, as I discovered while answering this question.

这篇关于使用namespace std进行删除会导致程序获取垃圾结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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