Code goes first:
void foo(int x)
{
void bar(int); //is this forward-decl legal?
bar(x);
}
void bar(int x)
{
//do stuff
}
In the code above, foo calls bar, usually I put the forward-decl of bar outside of foo, like this:
void bar(int);
void foo(int x)
{
bar();
}
First, I think it’s OK to put bar‘s forward-decl inside foo, right?
Second, consider this, if bar is a static function like this:
static void bar(int x)
{
//do stuff
}
Then how should I forward-declare it? I mean should the forward-decl take or omit the static?
Yes it’s legal to put a forward-declaration inside another function. Then it’s only usable in that function. And the namespace of the function you put it inside will be used, so make sure that matches.
The Standard says: “The linkages implied by successive declarations for a given entity shall agree.” (section 7.1.2). So yes, the prototype must be
staticalso. However, it doesn’t look like putting a prototype of astaticlinkage function inside another function is allowed at all. “There can be nostaticfunction declarations within a block” (same section).