Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 792
Danra Avatar asked Feb 12 '17 15:02

Danra


People also ask

Is Cmath a standard C++ library?

<cmath> is a standard library header of C++ which is extended from C header <math. h> and comes with namespace std . Since C++ 17, special mathematical functions were merged into the standard from TR1 and linear interpolation function (C++ 20) which were included inside <cmath> header.

Is math H part of STL?

h. math. h is a header file in the standard library of the C programming language designed for basic mathematical operations.


1 Answers

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.

like image 103
Potatoswatter Avatar answered Nov 22 '22 16:11

Potatoswatter