Every constexpr
function is pure, but not every pure function can or should be constexpr
.
[Examples involving constexpr
function templates are misleading, since function templates are not functions, they're patterns by which the compiler can generate functions. The outcome of function templates, their specialisations, are functions and they will be constexpr
iff possible.]
A pure function is one that only depends on its arguments, or other constant state. That's pretty much what a constexpr
function is. In addition, constexpr
functions must be defined (not only declared) prior to their first use (recursion seems to be allowed, though), and must consist of only the return statement. That's enough to make the allowed subset Turing-complete, but the result is not necessarily the most efficient form at runtime.
Which brings us to the mathematical functions. You can probably implement constexpr
sqrt()
or sin()
, but they would have to use a recursive implementation that the compiler can evaluate at compile-time, whereas at runtime, these would be better implemented in one assembler operation. Since constexpr
uses of sqrt()
and sin()
are few and far apart, its better to maximise runtime performance instead, which requires a form that isn't constexpr
able.
You may wonder why you can't write one constexpr
version of a function and one that's used at runtime, and I'd agree that would be nice to have, but the standard says you can't overload on constexpr
ness. Maybe in C++17...