Constexpr Math Functions

后端 未结 3 546
粉色の甜心
粉色の甜心 2020-11-28 09:34

So noticed from this page that none of the math functions in c++11 seems to make use of constexpr, whereas I believe all of them could be. So that leaves me with two questio

3条回答
  •  一整个雨季
    2020-11-28 10:02

    From "The C++ Programming Language (4th Edition)", by B. Stroustrup, describing C++11:

    "To be evaluated at compile time, a function must be suitably simple: a constexpr function must consist of a single return-statement; no loops, and no local variables are allowed. Also, a constexpr function may not have side effects."

    Which means that it must be inline, without for, while and if statements and local variables. Side effects are also forbidden (ex: changing of errno). Another problem is that most of math functions are FPU instructions which are not represented in pure c/c++ (they are written in assembler code). That's why non of cmath function is declared as constexpr.

提交回复
热议问题