The keyword constexpr
in the function definition tells the compiler that this function may be executed at compile-time if the all the arguments and variables are known at the compile-time itself. There is no such guarantee, though, for example when some of the values can be known only at runtime in which case the function will be executed at runtime.
However, it has nothing to do with pure or impure, since these terms imply that the output depends on the inputs only, and no matter how many times you call the function with the same values of input parameters, the output will be same everytime, irrespective of whether it is computed at compile-time or runtime.
Example,
constexpr int add(int a, int b) { return a + b; } //pure!
const int a = 2, b = 3; //const
int c = 2, d = 3; //non-const
//we may read update c and d here!
const int v1 = add(2,3); //computed at compile-time
const int v2 = add(a,3); //computed at compile-time
const int v3 = add(2,b); //computed at compile-time
const int v4 = add(a,b); //computed at compile-time
const int v3 = add(c,3); //computed at runtime
const int v3 = add(c,b); //computed at runtime
const int v3 = add(a,d); //computed at runtime
const int v3 = add(c,d); //computed at runtime
Note that here add
is a pure function irrespective of whether it is computed at compile-time or runtime.