From what I gather from this answer, a constexpr
function's result is not a constant-expression if the function has not been declared yet. What surprises me is the following code snippet :
constexpr int f();
constexpr int g() {
return f();
}
constexpr int f() {
return 42;
}
int main() {
constexpr int i = g();
return i;
}
This compiles without trouble and works. Moving f
's definition past main triggers error: 'constexpr int f()' used before its definition
, as I would expect.
I presume that it works because f
has been defined before the call to g
, thus both calls are constant expressions.
Why are f()
and g()
apparently constant-expressions, even though f
isn't defined when it is called by g
? How is this described by the Standard?
I have tested this on Coliru's GCC 6.1.0 and Clang 3.8.0.