In C, one can of course declare and define functions. And this interacts with default constructor calls from C++ to yield the most vexing parse problem. Say I want to declare a function foo, define a function bar and create an instance t, I might mistakenly write this (having Python or Java in mind):
T foo();
T bar() {
T t();
}
This will not do what I want, it will declare a function t with no arguments (or perhaps arbitrary many ones as there is no void written in there, not sure) and return type T. I would get a T instance with T t;.
Why is this syntax even allowed? What is the point of declaring a function just within a function? The linker will have to resolve this at link time anyway, and then one could just declare that function in the file scope. Is there some edge case that I am missing?
Not a formal explanation but just a use-case in which I would use the function-scope declaration.
Imagine you have a C header file offering an
inline staticfunction, but in specific cases this function should rely on an internal helper function which is not part of the API.There is probably an implementation file coming with this header-file.
Using this header-file provides the expected function but not the internal one (except if you ignore the warnings).
Without the ability to declare the internal function at function-scope, we would have had to declare it at global-scope in the header file, and that would have made it accessible without any warning.