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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:27:07+00:00 2026-05-25T21:27:07+00:00

What is a lambda expression in C++11? When would I use one? What class

  • 0

What is a lambda expression in C++11? When would I use one? What class of problem do they solve that wasn’t possible prior to their introduction?

A few examples, and use cases would be useful.

  • 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-25T21:27:08+00:00Added an answer on May 25, 2026 at 9:27 pm

    The problem

    C++ includes useful generic functions like std::for_each and std::transform, which can be very handy. Unfortunately they can also be quite cumbersome to use, particularly if the functor you would like to apply is unique to the particular function.

    #include <algorithm>
    #include <vector>
    
    namespace {
      struct f {
        void operator()(int) {
          // do something
        }
      };
    }
    
    void func(std::vector<int>& v) {
      f f;
      std::for_each(v.begin(), v.end(), f);
    }
    

    If you only use f once and in that specific place it seems overkill to be writing a whole class just to do something trivial and one off.

    In C++03 you might be tempted to write something like the following, to keep the functor local:

    void func2(std::vector<int>& v) {
      struct {
        void operator()(int) {
           // do something
        }
      } f;
      std::for_each(v.begin(), v.end(), f);
    }
    

    however this is not allowed, f cannot be passed to a template function in C++03.

    The new solution

    C++11 introduces lambdas allow you to write an inline, anonymous functor to replace the struct f. For small simple examples this can be cleaner to read (it keeps everything in one place) and potentially simpler to maintain, for example in the simplest form:

    void func3(std::vector<int>& v) {
      std::for_each(v.begin(), v.end(), [](int) { /* do something here*/ });
    }
    

    Lambda functions are just syntactic sugar for anonymous functors.

    Return types

    In simple cases the return type of the lambda is deduced for you, e.g.:

    void func4(std::vector<double>& v) {
      std::transform(v.begin(), v.end(), v.begin(),
                     [](double d) { return d < 0.00001 ? 0 : d; }
                     );
    }
    

    however when you start to write more complex lambdas you will quickly encounter cases where the return type cannot be deduced by the compiler, e.g.:

    void func4(std::vector<double>& v) {
        std::transform(v.begin(), v.end(), v.begin(),
            [](double d) {
                if (d < 0.0001) {
                    return 0;
                } else {
                    return d;
                }
            });
    }
    

    To resolve this you are allowed to explicitly specify a return type for a lambda function, using -> T:

    void func4(std::vector<double>& v) {
        std::transform(v.begin(), v.end(), v.begin(),
            [](double d) -> double {
                if (d < 0.0001) {
                    return 0;
                } else {
                    return d;
                }
            });
    }
    

    "Capturing" variables

    So far we’ve not used anything other than what was passed to the lambda within it, but we can also use other variables, within the lambda. If you want to access other variables you can use the capture clause (the [] of the expression), which has so far been unused in these examples, e.g.:

    void func5(std::vector<double>& v, const double& epsilon) {
        std::transform(v.begin(), v.end(), v.begin(),
            [epsilon](double d) -> double {
                if (d < epsilon) {
                    return 0;
                } else {
                    return d;
                }
            });
    }
    

    You can capture by both reference and value, which you can specify using & and = respectively:

    • [&epsilon, zeta] captures epsilon by reference and zeta by value
    • [&] captures all variables used in the lambda by reference
    • [=] captures all variables used in the lambda by value
    • [&, epsilon] captures all variables used in the lambda by reference but captures epsilon by value
    • [=, &epsilon] captures all variables used in the lambda by value but captures epsilon by reference

    The generated operator() is const by default, with the implication that captures will be const when you access them by default. This has the effect that each call with the same input would produce the same result, however you can mark the lambda as mutable to request that the operator() that is produced is not const.

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

Sidebar

Related Questions

I am new to System.Action<T> and Lambda expression. Here is one case I would
How would I go about joining two lambda expressions like theese: Expression<Func<string, bool>> expr1
If I use a lambda expression like the following // assume sch_id is a
Is it possible to pass a lambda expression to a secondary AppDomain as a
how can I create a dynamic lambda expression to pass to use in my
Would it work to use Expression<Func<T>> or Func<T> as keys in a dictionary? For
Not that I would want to use this practically (for many reasons) but out
Is it possible to assign an Lambda Expression (action?) to a property of my
I need to pass in a where lambda expression that'll be used in a
Why does this lambda expression not compile? Action a = () => throw new

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.