将isnan移植到c ++ 11

Dan*_*can 12 c++ c++11

当我将编译器版本从gcc 4.6切换到gcc 4.8时,我得到以下错误错误:调用重载'isnan(double)'是不明确的.

这是因为在c ++ 11中有不同的函数声明:C:int isnan(double)C++ 11:bool isnan(double)

来自cpluplus:

  • 在C中,这是作为返回int值的宏实现的.x的类型应为float,double或long double.
  • 在C++中,它使用每个浮点类型的函数重载来实现,每个浮点类型返回一个bool值.

我怎样才能解决这个问题?

jua*_*nza 14

虽然你可以通过不说using namespace std;到处来解决这个问题,你可以通过明确使用来避免它std::isnan:

#include <cmath>
#include <iostream>

int main()
{
  double x = ....;
  std::cout << std::boolalpha;
  std::cout << std::isnan(x) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

  • @wazza比破坏代码更合适。使用命名空间标准;是解决问题的良方。我认为花时间从代码中清除它的时间是很花时间的。 (5认同)