see code below, f() is defined below main function is regarded as ill-formed ? could anyone give me an explanation for this ?
constexpr int f ();
void indirection ();
int main () {
constexpr int n = f (); // ill-formed, `int f ()` is not yet defined
indirection ();
}
constexpr int f () {
return 0;
}
void indirection () {
constexpr int n = f (); // ok
}
The C++14 standard provides the following code snippet (shortened by me for convenience):
The solution is to move the definition of
square
above the the declaration ofsmall
.From the above we can get to the conclusion that it's fine to forward declare
constexpr
functions, but their definitions have to be available prior to their first use.