Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C++11 have any support for local functions?

Tags:

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?

like image 444
Mordachai Avatar asked Nov 09 '11 19:11

Mordachai


People also ask

Can you nest functions in C?

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.

What is lambda function in C++11?

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.

What is local function in C#?

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.

Can you have functions within functions in C?

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.


2 Answers

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 } 
like image 117
rodrigo Avatar answered Sep 20 '22 12:09

rodrigo


There are no local functions in C++11.

But there are lambdas.

And your local type can have member functions!

like image 25
Bo Persson Avatar answered Sep 18 '22 12:09

Bo Persson