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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:04:49+00:00 2026-06-07T01:04:49+00:00

I read this explanations on MSDN pages, for advantages of lambda expression over functor

  • 0

I read this explanations on MSDN pages, for advantages of lambda expression over functor and function pointer. What is meant by ability to ‘retain state’? Is it related to capability to capture some variables by ref or value in enclosing scope?

http://msdn.microsoft.com/en-us/library/dd293608.aspx

When writing code, you probably use function pointers and function
objects to solve problems and perform calculations. Both function
pointers and function objects have advantages and disadvantages:
function pointers involve minimal syntactic overhead, but they do not
retain state within a scope; function objects can maintain state, but
they require the syntactic overhead of a class definition.

Lambda expressions are a programming technique that combines the
benefits of function pointers and function objects and that avoids
their disadvantages. Lambda expressions are flexible and can maintain
state, just like function objects, and their compact syntax removes
the need for a class definition, which function objects require.
Lambda expressions enable you to write code that is less cumbersome
and less prone to errors than an equivalent function object.

The following examples compare the use of a lambda expression to the
use of a function object. The first example uses a lambda expression
to print to the console whether each element in a vector object is
even or odd. The second example uses a function object to accomplish
the same task.

Could you point at some relevant reference on topics scope, state, maintain state, advantage of lambda expressions?

  • 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-07T01:04:51+00:00Added an answer on June 7, 2026 at 1:04 am

    Functors and lambdas both have this ability over normal functions. It is the ability to remember stuff between function calls. Normal functions have static variables, but those are globally unique, which is no good if you want seperate function objects with their own unique state. Here’s an example functor class:

    class Counter
    {
        int n;
    public:
        Counter() :n(0) {}
        int operator()() { return n++; }    
    };
    

    With this class, I can create an instance that acts as a function, and each time you call it, it remembers the previous value of n, e.g.

    Counter x;
    cout << x() << '\n';
    cout << x() << '\n';
    cout << x() << '\n';
    

    You can’t do that with normal functions. But you can do it with lambdas:

    int n = 0;
    auto x = [=]() mutable { return n++; };
    cout << x() << '\n';
    cout << x() << '\n';
    cout << x() << '\n';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read hundreds of explanations on this in java and I'm really having trouble
I have read over this which sort of gives an explanation of when you'd
I just read through this article on named function expressions and their incompatibilities with
I read this question in stackoverflow. The excerpt answer provided by bbum is below:
I read this post where the author advices to store session files in a
I read this code in a library which is used to display a bitmap
I read this article : http://lwuit.blogspot.in/2008/06/lightweight-text-input-in-lwuit.html This article talks of autocomplete textfield embedded on
I read this article on how to use keytool to generate CSRs for your
I read this story on slashdot today where they announce a new parallel programming
Having read this link on RBAR and this , my understanding of RBAR amounts

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.