Alex Rivera | Logout

Relation between constexpr and pure functions

Asked 2011-03-28T16:56:39.673
23

Am I right, that:

  • Any function defined with constexpr is a pure function, and
  • Any pure function can be and must be defined with constexpr if it's not very expensive for compiler.

And if so, why arent <cmath>'s functions defined with constexpr ?

Edit
Report

1 Answer

10

In addition to the previous answers: constexpr on a function restricts its implementation greatly: its body must be visible to the compiler (inline), and must consist only of a single return statement. I'd be surprised if you could implement sqrt() or sin() correctly and still meet that last condition.

answered 2011-03-28T17:29:09.370

Your Answer