Danra
Danra

Reputation: 9906

Why doesn't the C++ standard library provide constexpr versions of the cmath functions?

We have constexpr functions since C++11, and they have been getting less restricted since with every new standard (14, 1z).

Yet, the most obvious functions in STL which could be made constexpr, the cmath/math.h functions, still have no constexpr version in any standard library implementation AFAIK.

Is this just in the backlog of the C++ standard, or is there any other reason why we still don't have constexpr versions of these functions?

Upvotes: 7

Views: 1733

Answers (1)

Potatoswatter
Potatoswatter

Reputation: 137800

It hasn't been standardized yet. An initial proposal was submitted just last week, but only covering utility and linear operations and not any transcendental functions. Math is hard and floating-point math is complicated. For example, implementations don't allow overflows to infinity in constexpr, but this isn't yet clearly standardized.

The compiler's constexpr interpreter would have to special-case the math library interface, since unlike the rest of the standard library, it can't see its implementation.

GCC does offer constant evaluation of math functions as a nonconforming extension.

Upvotes: 7

Related Questions