mardi 26 mai 2015

division by zero not constexpr

When compiling this:

constexpr double x {123.0};
constexpr double y = x / 0.0;
std::cout << x << " / 0 = " << y << "\n";

The compiler (gcc 4.9.2, -std=c++11 or c++14) fails, giving error:

'(1.23e+2 / 0.0)' is not a constant expression constexpr double y = x / 0.0;

How is the result (Inf) relevant when deciding if y can be a constexpr or not?

For reference, this seems to be the way to do it:

static constexpr double z = std::numeric_limits<double>::quiet_NaN();
static constexpr double w = std::numeric_limits<double>::infinity();

Aucun commentaire:

Enregistrer un commentaire