编译时:
constexpr double x {123.0};
constexpr double y = x / 0.0;
std::cout << x << " / 0 = " << y << "\n";
Run Code Online (Sandbox Code Playgroud)
编译器(gcc 4.9.2,-std = c ++ 11或c ++ 14)失败,给出错误:
(1.23e+2 / 0.0)' is not a constant expression
constexpr double y = x / 0.0;
Run Code Online (Sandbox Code Playgroud)
在决定是否y可以成为constexpr时,结果(Inf)如何相关?
作为参考,这似乎是这样做的方式:
static constexpr double z = std::numeric_limits<double>::quiet_NaN();
static constexpr double w = std::numeric_limits<double>::infinity();
Run Code Online (Sandbox Code Playgroud)