Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6785995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:11:36+00:00 2026-05-26T17:11:36+00:00

Now that there are lambdas in C++, it seems really silly that I cannot

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:11:37+00:00Added an answer on May 26, 2026 at 5:11 pm

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are lots of Fluent implementations out there now that work with Lambdas to
Now that I know I can no longer communicate with Twitter mashups out there,
Update: Now that it's 2016 I'd use PowerShell for this unless there's a really
I appreciate that there are now many mechanisms in dotnet to deal with XML
Now that it's clear what a metaclass is , there is an associated concept
Does anyone know if there's a windows Python executable creator program available now that
I had this doubt for sometime now, some people say that there's no such
I made an OCX with Delphi 2007. Now my customer claims that there is
Now I know that bigint is 2^64; that is, more atoms than there are
Is there any easy way to tell perl now ignore everything that is printed?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.