【发布时间】:2016-11-21 08:34:42
【问题描述】:
我正在尝试编写一个在 MSVC 2010 中使用std::isnan() 的程序。我包含cmath 但不幸的是编译器返回错误:
isnan 不是 std 命名空间的一部分
MSVC 2010 是否支持 std (AKA C++11) 中的此功能?
【问题讨论】:
我正在尝试编写一个在 MSVC 2010 中使用std::isnan() 的程序。我包含cmath 但不幸的是编译器返回错误:
isnan 不是 std 命名空间的一部分
MSVC 2010 是否支持 std (AKA C++11) 中的此功能?
【问题讨论】:
std::isnan 是在<cmath>:http://en.cppreference.com/w/cpp/numeric/math/isnan
您的问题可能是 VS2010 对 C++11 的支持很差。我建议使用 VS2015,在这方面要好得多。
可以看出hereVS2010只有_isnan。
【讨论】:
不幸的是,Visual Studio 的 C++11 支持直到 2015 版本才支持 complete,因此您将无法使用 C++ std::isnan 功能。有趣的是,有一个 C99 isnan 宏,但它的实现已定义,而 VS 2010 似乎没有任何这些宏。不过幸运的是,MS 系列编译器确实具有其特定于 MS 的 _ 版本的 _isnan 功能。所以你可以这样写自己的isnan:
#include <iostream>
#include <cmath>
#include <cfloat>
#include <limits>
namespace non_std
{
template < typename T >
bool isnan(T val)
{
#if defined(_WIN64)
// x64 version
return _isnanf(val) != 0;
#else
return _isnan(val) != 0;
#endif
}
}
int main(int argc, char** argv)
{
float value = 1.0f;
std::cout << value << " is " <<
(non_std::isnan(value) ? "NaN" : "NOT NaN") << std::endl;
if (std::numeric_limits<float>::has_quiet_NaN) {
value = std::numeric_limits<float>::quiet_NaN();
std::cout << value << " is " <<
(non_std::isnan(value) ? "NaN" : "NOT NaN") << std::endl;
}
return 0;
}
在我的机器上,这会产生输出:
1 不是 NaN
1.#QNAN 是 NaN
请注意,_isnanf 是针对 64 位应用程序的,_WIN64 宏可能不一定要定义,因此如果您以 64 位为目标,请务必进行调整。
希望能有所帮助。
【讨论】: