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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:12:17+00:00 2026-05-29T05:12:17+00:00

What is the best practice to use when one needs to store a lambda

  • 0

What is the best practice to use when one needs to store a lambda as a class member so that its invocation can be deferred? More specifically, is it safe to store the lambda passed to the class deferred_lambda in the code listing below as a reference? If not, would it be safe if I were to store the the lambda in deferred_lambda as a value instead of as a reference?

Finally, can I expect to incur a performance penalty in comparison to a regular function call with g++ for storing the lambda as a class member in this way? That is, would using deferred_lambda.invoke() be slower than a call to operator() on some dummy struct that would implement the same operations?

With g++, I noticed that the size of the lambda increases as I use more captured variables. I suppose that this is to be expected, since to my understanding, the compiler internally generates a struct for the lambda that contains the necessary captured variables as members. This observation is what led to the question that I am now asking, since storing lambdas by value may be more expensive in terms of time and memory than storing references to them would.

template <class Func>
class deferred_lambda
{
    Func& func_;
public:
    deferred_lambda(Func func) : func_(func) {}
    void invoke() { func_(); }
};

template <class Func>
deferred_lambda<Func> defer_lambda(Func func)
{
    return deferred_lambda(func);
}

void foo()
{
    int a, b, c;
    auto x = defer_lambda([&]() { a = 1; b = 2; c = 3; });
} 
  • 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-29T05:12:18+00:00Added an answer on May 29, 2026 at 5:12 am

    More specifically, is it safe to store the lambda passed to the class deferred_lambda in the code listing below as a reference?

    No. That would be a dangling reference after defereed_lambda() has finished.

    If not, would it be safe if I were to store the the lambda in deferred_lambda as a value instead of as a reference?

    Yes. But you still have to ensure variables captured by reference still live when executing the lambda.

    That is, would using deferred_lambda.invoke() be slower than a call to operator() on some dummy struct that would implement the same operations?

    Probably not, there’s no reason for it.

    This observation is what led to the question that I am now asking, since storing lambdas by value may be more expensive in terms of time and memory than storing references to them would.

    It still has to be stored somewhere…

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

Sidebar

Related Questions

Since I can't use Microsoft as an example for best practice since their exception
When I use itoa() it needs a char* _DstBuff, what is the best practice
Is it a best practice to use stored procedure for every single SQL call
Best practice is to use unique ivs, but what is unique? Is it unique
I am looking for guidance regarding the best practice around the use of the
What is the common way (or best practice) to optionally use JNI? E.g. I
Using SQL Server, which is the fastest or best practice method to use for
As I understand it, the best practice for generating salts is to use some
What are the best practices for using use cases to model system behavior that
I am working on a project that has one page that needs to make

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.