小编Wie*_*ebe的帖子

浮点除以零而不是constexpr

编译时:

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)

undefined-behavior constexpr c++11 c++14

4
推荐指数
1
解决办法
321
查看次数

标签 统计

c++11 ×1

c++14 ×1

constexpr ×1

undefined-behavior ×1