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 8970539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:44:24+00:00 2026-06-15T17:44:24+00:00

From my experience it seems that either: A lambda expression created inside a function

  • 0

From my experience it seems that either:

  • A lambda expression created inside a function call is destroyed just after the invocation
  • Calling a function that expects a std::function creates a temporary object (std::function) out of the lambda, and that object is destroyed after invocation

This behavior can be observed with the following snippet of code:

const function<void()>* pointer;

void a(const function<void()> & f)
{
    pointer = &f;
}

void b()
{
    (*pointer)();
}

int main()
{
    int value = 1;
    std::cout << &value << std::endl;

    // 1: this works    
    function<void()> f = [&] () { std::cout << &value << std::endl; };
    a(f);

    // 2: this doesn't
    a([&] () { std::cout << &value << std::endl; });

    /* modify the stack*/
    char data[1024];
    for (int i = 0; i < 1024; i++)
        data[i] = i % 4; 

    b();

    return 0;
}

What exactly s actually happening in the second case?
Is there a correct way to call a() without creating an explicit std::function object?

Edit::
This both versions (1 and 2) compile just right but result in different outputs:

Version 1:

0x7fffa70148c8
0x7fffa70148c8

Version 2:

0x7fffa70148c8
0
  • 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-15T17:44:25+00:00Added an answer on June 15, 2026 at 5:44 pm

    If you create a temporary, it will be gone at the end of the line. This means storing a pointer to it is a bad idea, as you correctly stated.

    If you want to store a pointer to a std::function (or anything else really), you need to make sure it’s lifetime doesn’t end before you stop using the pointer. This means that you really do need a named object of type std::function.

    As to what is happening in the second case: You create a temporary lambda to be passed to the function. Since the function expects a std::function, a temporary std::function will be created from the lambda. Both of those will be destroyed at the end of the line. Therefore you now have a pointer to an already destroyed temporary, which means that trying to use the pointed to object will bring you firmly into undefined behaviour territory.

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

Sidebar

Related Questions

It seems like buttons and links are interchangeable these days. From a user-experience perspective,
From previous experience I had been under the impression that it's perfectly legal (though
EDIT: after reading this http://projects.scipy.org/numpy/ticket/1322 it seems that the NumPy version I am using
From your experience, what is the most accurate open-source Optical Character Recognition (OCR) library/software
From hard experience I've found it useful to occasionally save the state of my
I have two questions: From personal experience, what free blog engine is the best
Does anyone experience to get depth map from stereo pair? I wonder if are
Coming from PHP with some Java Struts 2.0 experience any tips/tricks on learning GWT?
im coming from unix / server / c++ /java background with no GUI experience
Does anyone have any experience with doing mail merge from Java on a word

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.