问题描述
对于Mingw 4.7.2,我有一个库,因为调用 isnan
而无法编译。
如果我使用 std :: isnan
,编译器说一切都很好,而且我设法编译我的文件。
但如果我检查(编辑:,但也许我还应该检查: - )), std ::
似乎没有必要。
一般来说,对于每种情况,有一个通用的方法来理解何时放置 std:
$ b
确实,问题的起源是有多个头部包含,并且一些包含的头包括< cmath>
,而这个cpp文件尝试包括< math.h>
(当已经包括< cmath>
时)。
这取决于您包含的标题。如果你包括C头< math.h>
(它是C ++的一部分,虽然标记为deprecated),然后你可以使用非限定C函数, c $ c> isnan 。如果你另一方面包括C ++头< cmath>
,你只有保证,它带来所有的功能从
插入 std
命名空间,因此您必须正确地限定它们,例如 std: :isnan
(或使用某种使用
指令)。不幸的是,当包括< cmath> $ c $时,允许实现,但不需要将这些函数带入全局命名空间c>(因此它是许多在我的机器上工作 - C ++的原因之一,以及许多人编写代码,如你只是试图编译失败的原因)。
总而言之:< math.h>
并使用 isnan
或包含< cmath>
并使用 std :: isnan
,一切都是不可移植的。当然这一切都适用于任何其他C头文件及其各自的C ++版本。
EDIT:特定函数 isnan
仅支持自C ++ 11,并且在C ++ 98中不可用(这可能是您的困惑的一部分)。但这在这种情况下不会改变任何东西,因为在C ++ 98中既不是< cmath>
也不是< math.h>
(这是实际的C89 / C90头,而不是C99头包括C ++ 11包括)有这个功能,因为他们总是同步。所以这个库从你的问题可能尝试使用C ++ 98,同时从不同的C99实现(这不是一个特别好的想法,采取 isnan
它可能与C ++实现的C89 / C90部分冲突,甚至从未尝试过)。
With Mingw 4.7.2, I have a library that doesn't compile because of a call to isnan
.The compiler says "everything will be fine" if I use std::isnan
, and indeed I manage to compile my file.
But if I check here (Edit: but maybe I should have checked also here :-) ), the std::
doesn't seem to be necessary. If I add it, will the file be portable?
More in general, for each case is there a general way to understand when putting std::
is necessary (for portability), optional or to be avoided?
Edit
Indeed among the origins of the problem is that there are multiple header inclusions, and some of the included headers include <cmath>
, while this cpp file tries to include <math.h>
(when <cmath>
has already been included).
It depends on which header you include. If you include the C header <math.h>
(which is part of C++, albeit marked as deprecated), then you can use the unqualified C functions, like isnan
. If you on the other hand include the C++ header <cmath>
, you are only guaranteed that it brings all the functions from <math.h>
into the std
namespace and thus you have to properly qualify them, like std::isnan
(or use some kind of using
directive). Unfortunately an implementation is allowed but not required to bring those functions into the global namespace, too, when including <cmath>
(and thus it is one of the many "works on my machine"-incidences of C++ and the reason why many people write code like you just tried to compile unsuccessfully).
So to sum up: Either include <math.h>
and use isnan
or include <cmath>
and use std::isnan
, everything else is non-portable. Of course all this applies to any other C header and its respective C++ version, too.
EDIT: It should be noted though, that this particular function isnan
is only supported since C++11 and wasn't available in C++98 at all (which may be part of your confusion). But this doesn't change anything in this situation because in C++98 neither <cmath>
nor <math.h>
(which was the actual C89/C90 header back then and not the C99 header that C++11 includes) had this function, since they're always in-sync. So what this library from your question maybe tried was to use C++98 while taking the isnan
function from a different C99 implementation (which isn't a particularly good idea, as it might conflict with the C89/C90 parts of the C++ implementation, never even tried this though).
这篇关于isnan在std ::命名空间?一般来说,什么时候是std ::必需的,可选的还是要避免的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!