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?
No, you can't. We can't define functions inside other functions in C. With languages like JavaScript, Swift or Python it is pretty common to use nested functions.
In C++11 and later, a lambda expression—often called a lambda—is a convenient way of defining an anonymous function object (a closure) right at the location where it's invoked or passed as an argument to a function.
Local functions are private methods of a type that are nested in another member. They can only be called from their containing member. Local functions can be declared in and called from: Methods, especially iterator methods and async methods. Constructors.
No you can't have a nested function in C . The closest you can come is to declare a function inside the definition of another function. The definition of that function has to appear outside of any other function body, though.
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:
void foo(int x) { struct S { //... }; int Twice(int n, S *s) //Not allowed { return 2*n; } S s; int x = Twice(3, &s); //... }
Do:
void foo() { struct S { //... }; auto Twice = [](int x, S *s) -> int //Cool! { return 2*x; }; //Twice is actually a variable, so don't forget the ; S s; int x = Twice(3, &s); //... }
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):
void foo() { struct S {}; std::vector<S> vs; //error in C++98, ok in C++11 }
There are no local functions in C++11.
But there are lambdas.
And your local type can have member functions!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With