Now that there are lambdas in C++, it seems really silly that I cannot declare a local function…
e.g.:
I can declare a type in a function body, even initialize it as a table of values. But I cannot create a helper function that works with that data type – because I cannot declare a function in a function, and I cannot refer to that data type outside of the function because it’s only available in that scope.
There are times when it’s quite simple to pull the data type out of the function, and define my data type and helper functions there (local file scope) – but there are times when it’s not really a plausible solution – e.g. when initializing the table with inline lambdas that refer to local scope variables (or this).
Any idea whether support for local functions is coming, is already defined, or why that they’re difficult for compiler-writers to implement and hence aren’t a part of the standard?
There are no local functions, but they are not so useful without the closure, that is, access to the local variables. In any case you can emulate a local function with a lambda easy enough.
Instead of:
Do:
If the capture set is empty, (
[]) it can even be converted to an ordinary pointer-to-function, just like a real one!And AFAIK, lambdas can use local types without trouble. But, of course, public static member functions in that struct should work also fine.
And as an additional note, indirectly related to your question, what it is allowed in C++11 is to instantiate a template using a local type (that was forbidden in C++98):