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

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

问题描述

我有一个复杂的最小二乘拟合程序,我最近调试。我使用命名空间std来使用

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;

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

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.

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

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.

这些可能是您的代码的名称;或者他们是C库函数。许多(特别是< cmath> 中的)被重载用于各种类型,其中C库本身只声明一个单一的函数。它是未指定是否,一些或所有这些被转储到全局命名空间以及命名空间std

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);

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

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.

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

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天全站免登陆