The value of a const variable is or is not usable in a constant expression, depending on the variable type

Viewed 34853

The following code is fine:

constexpr double square_cstxpr(double x) { return x * x; }

int main() {
    const int test = 5;
    constexpr double result = square_cstxpr((double)test);
}

However, if the type of test is changed from const int to const double, g++ gives the following error: the value of 'test' is not usable in a constant expression.

See the code and output of g++ here: http://coliru.stacked-crooked.com/a/2fe9b176c2b23798

Could somebody explain that behavior?

2 Answers
Related