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

  • SEARCH
  • Home
  • 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 8591929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:38:02+00:00 2026-06-11T23:38:02+00:00

this piece of code is not something unknown to JS developers function get_counter() {

  • 0

this piece of code is not something unknown to JS developers

function get_counter()
{
    return (
        function() {
            var c = 0;
            return function() { return ++c; };
        })();
}

it basically creates a which creates different enumerators. So I was wondering if same thing can be done in C++11 with new lambda semantics? I ended up writing this piece of C++ which unfortunately does not compile!

int main()
{
    int c;
    auto a = [](){
        int c = 0;
        return [&](){
            cout << c++;
        };
    };
    return 0;
}

so I was wondering if there is a workaround to get it compiled and if there is how can compiler make this code run correctly? I mean it has to create separate enumerators but it should also collect garbage (unused c variables).

by the way I’m using VS2012 compiler and it generates this error:

Error   2   error C2440: 'return' : cannot convert from 'main::<lambda_10d109c73135f5c106ecbfa8ff6f4b6b>::()::<lambda_019decbc8d6cd29488ffec96883efe2a>' to 'void (__cdecl *)(void)'    c:\users\ali\documents\visual studio 2012\projects\test\test\main.cpp   25  1   Test
  • 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-06-11T23:38:03+00:00Added an answer on June 11, 2026 at 11:38 pm

    Your code has a bug in that it contains a dangling reference; the c reference will refer to the local variable in the outer lambda, which will be destroyed when the outer lambda returns.

    You should write it using a mutable by-value lambda capture:

    auto a = []() {
        int c = 0;
        return [=]() mutable {
            cout << c++;
        };
    };
    

    This relies on a post-standard extension to allow multiple statements in a return-type-deducing lambda; Is there a reason on not allowing lambdas to deduce the return type if it contains more than one statement? The easiest way to fix it is to supply a parameter so that the lambda contains only a single statement:

    auto a = [](int c) {
        return [=]() mutable {
            cout << c++;
        };
    };
    

    Unfortunately default parameters aren’t allowed in lambdas, so you’d have to call this as a(0). Alternatively at the cost of readability you could use a nested lambda call:

    auto a = []() {
        return ([](int c) {
            return [=]() mutable {
                cout << c++;
            };
        })(0);
    };
    

    The way this works is that when a executes the inner lambda copies all the referenced variables into an instance of its closure type, which here would be something like:

    struct inner_lambda {
        int c;
        void operator()() { cout << c++; }
    };
    

    The instance of the closure type is then returned by the outer lambda, and can be invoked and will modify its copy of c when called.

    Overall, your (fixed) code is translated to:

    struct outer_lambda {
        // no closure
        struct inner_lambda {
            int c;    // by-value capture
            // non-const because "mutable"
            void operator()() { cout << c++; }
        }
        // const because non-"mutable"
        inner_lambda operator()(int c) const {
            return inner_lambda{c};
        }
    };
    

    If you left c as a by-reference capture, this would be:

    struct outer_lambda {
        // no closure
        struct inner_lambda {
            int &c;    // by-reference capture
            void operator()() const { cout << c++; } // const, but can modify c
        }
        inner_lambda operator()(int c) const {
            return inner_lambda{c};
        }
    };
    

    Here inner_lambda::c is a dangling reference to the local parameter variable c.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
This piece of code used to work in MVC 1 but it does not
I just realized that this piece of code works well in Firefox but not
Using this piece of code in the head of a page: <script type=text/javascript> function
I have this piece of code: private void prepareContent() { log.info(do something); // success?
I just wrote this piece of code but I'm not quite happy about it.
I have this piece of code which works 90% of the time: $user_details=$fb->api_client->users_getInfo($fb_user, array('last_name','first_name','proxied_email'));
Since the retina display, suddenly this piece of drawing code seems to not work
Something that is called when it is extended. Eg. this piece of code: module
I ran this piece of code var m = new MarkdownSharp.Markdown(); var sz =

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.