By Nawaz

2011-10-02 14:58:12 8 Comments

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.


@Sachin Nale 2019-06-04 10:22:34

The lambda's in c++ are treated as "on the go available function". yes its literally on the go, you define it; use it; and as the parent function scope finishes the lambda function is gone.

c++ introduced it in c++ 11 and everyone started using it like at every possible place. the example and what is lambda can be find here

i will describe which is not there but essential to know for every c++ programmer

Lambda is not meant to use everywhere and every function cannot be replaced with lambda. It's also not the fastest one compare to normal function. because it has some overhead which need to be handled by lambda.

it will surely help in reducing number of lines in some cases. it can be basically used for the section of code, which is getting called in same function one or more time and that piece of code is not needed anywhere else so that you can create standalone function for it.

Below is the basic example of lambda and what happens in background.

User code:

int main()
  // Lambda & auto
  int member=10;
  auto endGame = [=](int a, int b){ return a+b+member;};


  return 0;


How compile expands it:

int main()
  int member = 10;

  class __lambda_6_18
    int member;
    inline /*constexpr */ int operator()(int a, int b) const
      return a + b + member;

    public: __lambda_6_18(int _member)
    : member{_member}


  __lambda_6_18 endGame = __lambda_6_18{member};
  endGame.operator()(4, 5);

  return 0;

so as you can see, what kind of overhead it adds when you use it. so its not good idea to use them everywhere. it can be used at places where they are applicable.

@Nawaz 2019-06-05 01:53:38

yes its literally on the go, you define it; use it; and as the parent function scope finishes the lambda function is gone .. what if the function returns the lambda to the caller?

@Nawaz 2019-06-05 01:57:08

It's also not the fastest one compare to normal function. because it has some overhead which need to be handled by lambda. Have you ever actually run any benchmark to support this claim? On the contrary, lambda + templates often produce fastest code possible.

@bruziuz 2015-06-03 16:40:16


Q: What is a lambda expression in C++11?

A: Under the hood, it is the object of an autogenerated class with overloading operator() const. Such object is called closure and created by compiler. This 'closure' concept is near with the bind concept from C++11. But lambdas typically generate better code. And calls through closures allow full inlining.

Q: When would I use one?

A: To define "simple and small logic" and ask compiler perform generation from previous question. You give a compiler some expressions which you want to be inside operator(). All other stuff compiler will generate to you.

Q: What class of problem do they solve that wasn't possible prior to their introduction?

A: It is some kind of syntax sugar like operators overloading instead of functions for custom add, subrtact operations...But it save more lines of unneeded code to wrap 1-3 lines of real logic to some classes, and etc.! Some engineers think that if the number of lines is smaller then there is a less chance to make errors in it (I'm also think so)

Example of usage

auto x = [=](int arg1){printf("%i", arg1); };
void(*f)(int) = x;

Extras about lambdas, not covered by question. Ignore this section if you're not interest

1. Captured values. What you can to capture

1.1. You can reference to a variable with static storage duration in lambdas. They all are captured.

1.2. You can use lambda for capture values "by value". In such case captured vars will be copied to the function object (closure).

[captureVar1,captureVar2](int arg1){}

1.3. You can capture be reference. & -- in this context mean reference, not pointers.

   [&captureVar1,&captureVar2](int arg1){}

1.4. It exists notation to capture all non-static vars by value, or by reference

  [=](int arg1){} // capture all not-static vars by value

  [&](int arg1){} // capture all not-static vars by reference

1.5. It exists notation to capture all non-static vars by value, or by reference and specify smth. more. Examples: Capture all not-static vars by value, but by reference capture Param2

[=,&Param2](int arg1){} 

Capture all not-static vars by reference, but by value capture Param2

[&,Param2](int arg1){} 

2. Return type deduction

2.1. Lambda return type can be deduced if lambda is one expression. Or you can explicitly specify it.

[=](int arg1)->trailing_return_type{return trailing_return_type();}

If lambda has more then one expression, then return type must be specified via trailing return type. Also, similar syntax can be applied to auto functions and member-functions

3. Captured values. What you can not capture

3.1. You can capture only local vars, not member variable of the object.

4. Сonversions

4.1 !! Lambda is not a function pointer and it is not an anonymous function, but capture-less lambdas can be implicitly converted to a function pointer.


  1. More about lambda grammar information can be found in Working draft for Programming Language C++ #337, 2012-01-16, 5.1.2. Lambda Expressions, p.88

  2. In C++14 the extra feature which has named as "init capture" have been added. It allow to perform arbitarily declaration of closure data members:

    auto toFloat = [](int value) { return float(value);};
    auto interpolate = [min = toFloat(0), max = toFloat(255)](int value)->float { return (value - min) / (max - min);};

@GetFree 2017-04-15 08:00:24

This [&,=Param2](int arg1){} doesn't seem to be valid syntax. The correct form would be [&,Param2](int arg1){}

@bruziuz 2017-04-16 13:02:45

Thanks. First I tried to compile this snippet. And it seems strange assymetry in allowable modificators in capture list // g++ -std=c++11 main.cpp -o test_bin; ./test_bin #include <stdio.h> int main() { #if 1 { int param = 0; auto f=[=,&param](int arg1) mutable {param = arg1;}; f(111); printf("%i\n", param); } #endif #if 0 { int param = 0; auto f=[&,=param](int arg1) mutable {param = arg1;}; f(111); printf("%i\n", param); } #endif return 0; }

@bruziuz 2017-04-16 13:07:05

Looks that new line in not supported in comment. Then I opened 5.1.2 Lambda expressions, p.88, "Working Draft, Standard for Programming Language C ++", Dcoument Number: #337, 2012-01-16. And looked into grammar syntax. And you're right. There is no exist such thing like capture via "=arg"

@bruziuz 2017-04-16 13:17:02

Big Thanks, fixed it in description and also acquire new knowledge w.r.t. to it.

@Flexo 2011-10-02 15:21:05

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] capture by reference
  • [&] captures all variables used in the lambda by reference
  • [=] captures all variables used in the lambda by value
  • [&, epsilon] captures variables like with [&], but epsilon by value
  • [=, &epsilon] captures variables like with [=], but 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.

@Yakk - Adam Nevraumont 2013-03-13 13:44:42

Does mutable make operator() non-const, or does it make the copies of the local variables mutable? Conclusion it simply makes the state of the lambda mutable, operator() remains const.

@Johannes Schaub - litb 2013-03-31 22:17:16

@Yakk you have been trapped. lambdas without a capture have an implicit conversion to function type pointers. the conversion function is const always...

@Yakk - Adam Nevraumont 2013-04-01 00:55:59

@JohannesSchaub-litb oh sneaky -- and it happens when you invoke () -- it is passed as a zero-argument lambda, but because () const doesn't match the lambda, it looks for a type conversion which allows it, which includes implicit-cast-to-function-pointer, and then calls that! Sneaky!

@user253751 2014-03-09 01:39:54

Interesting - I originally thought that lambdas were anonymous functions rather than functors, and was confused about how captures worked.

@Evert Heylen 2015-04-10 16:15:38

If you want to use lambdas as variables in your program, you can use: std::function<double(int, bool)> f = [](int a, bool b) -> double { ... }; But usually, we let the compiler deduce the type: auto f = [](int a, bool b) -> double { ... }; (and don't forget to #include <functional>)

@fluffy 2015-08-18 07:34:11

It seems like there should be a note about how reference-based capture should be used with caution when storing the lambda in a variable that can potentially exist beyond the scope of what's being referenced.

@Flexo 2015-08-18 07:35:44

That's always true of references.

@Lundin 2015-12-17 07:32:52

I suppose not everyone understands why return d < 0.00001 ? 0 : d; is guaranteed to return double, when one of the operands is an integer constant (it is because of an implicit promotion rule of the ?: operator where the 2nd and 3rd operand are balanced against each other through the usual arithmetic conversions no matter which one that gets picked). Changing to 0.0 : d would perhaps make the example easier to understand.

@idclev 463035818 2016-09-03 11:09:38

quite old post. However, in your first example I dont understand "f cannot be passed to a template function in C++03". What is the difference to func where f is passed to a template function. Well, while writing this I realize that the difference is that in func f is has a unnamed type. Nevertheless I think the (already very helpful) answer could be improved by adding this detail.

@Flexo 2016-09-09 10:50:55

Difference is that f lives inside a function - the rules forbid doing that.

@Guy Avraham 2017-04-18 16:39:18

A thing to note here regarding motivation of usage in lambdas, is in asynchronous programming, as far as I understand.

@Rhialto supports Monica 2018-01-16 18:45:57

Just a nit: the first example doesn't need the struct f; you can simply define a function directly. That does away with part of the overkill silliness.

@Mohammad Mamun Hossain 2018-02-26 07:10:50

How to pass an array in the capture clause?

@Flexo 2018-02-26 08:27:37

@MohammadMamunHossain use std::array array instead of raw arrays and then it becomes trivial. (Which is good advice in most cases in C++ anyway)

@Misgevolution 2015-11-23 09:16:40

Well, one practical use I've found out is reducing boiler plate code. For example:

void process_z_vec(vector<int>& vec)
  auto print_2d = [](const vector<int>& board, int bsize)
    for(int i = 0; i<bsize; i++)
      for(int j=0; j<bsize; j++)
        cout << board[bsize*i+j] << " ";
      cout << "\n";
  // Do sth with the vec.
  // Do sth else with the vec.

Without lambda, you may need to do something for different bsize cases. Of course you could create a function but what if you want to limit the usage within the scope of the soul user function? the nature of lambda fulfills this requirement and I use it for that case.

@gbk 2016-11-09 11:02:16

One of the best explanation of lambda expression is given from author of C++ Bjarne Stroustrup in his book ***The C++ Programming Language*** chapter 11 (ISBN-13: 978-0321563842):

What is a lambda expression?

A lambda expression, sometimes also referred to as a lambda function or (strictly speaking incorrectly, but colloquially) as a lambda, is a simplified notation for defining and using an anonymous function object. Instead of defining a named class with an operator(), later making an object of that class, and finally invoking it, we can use a shorthand.

When would I use one?

This is particularly useful when we want to pass an operation as an argument to an algorithm. In the context of graphical user interfaces (and elsewhere), such operations are often referred to as callbacks.

What class of problem do they solve that wasn't possible prior to their introduction?

Here i guess every action done with lambda expression can be solved without them, but with much more code and much bigger complexity. Lambda expression this is the way of optimization for your code and a way of making it more attractive. As sad by Stroustup :

effective ways of optimizing

Some examples

via lambda expression

void print_modulo(const vector<int>& v, ostream& os, int m) // output v[i] to os if v[i]%m==0
        [&os,m](int x) { 
           if (x%m==0) os << x << '\n';

or via function

class Modulo_print {
         ostream& os; // members to hold the capture list int m;
         Modulo_print(ostream& s, int mm) :os(s), m(mm) {} 
         void operator()(int x) const
             if (x%m==0) os << x << '\n'; 

or even

void print_modulo(const vector<int>& v, ostream& os, int m) 
     // output v[i] to os if v[i]%m==0
    class Modulo_print {
        ostream& os; // members to hold the capture list
        int m; 
           Modulo_print (ostream& s, int mm) :os(s), m(mm) {}
           void operator()(int x) const
               if (x%m==0) os << x << '\n';

if u need u can name lambda expression like below:

void print_modulo(const vector<int>& v, ostream& os, int m)
    // output v[i] to os if v[i]%m==0
      auto Modulo_print = [&os,m] (int x) { if (x%m==0) os << x << '\n'; };

Or assume another simple sample

void TestFunctions::simpleLambda() {
    bool sensitive = true;
    std::vector<int> v = std::vector<int>({1,33,3,4,5,6,7});

         [sensitive](int x, int y) {
             printf("\n%i\n",  x < y);
             return sensitive ? x < y : abs(x) < abs(y);

    for_each(v.begin(), v.end(),
             [](int x) {
                 printf("x - %i;", x);

will generate next











0 sortedx - 1;x - 3;x - 4;x - 5;x - 6;x - 7;x - 33;

[] - this is capture list or lambda introducer: if lambdas require no access to their local environment we can use it.

Quote from book:

The first character of a lambda expression is always [. A lambda introducer can take various forms:

[]: an empty capture list. This implies that no local names from the surrounding context can be used in the lambda body. For such lambda expressions, data is obtained from arguments or from nonlocal variables.

[&]: implicitly capture by reference. All local names can be used. All local variables are accessed by reference.

[=]: implicitly capture by value. All local names can be used. All names refer to copies of the local variables taken at the point of call of the lambda expression.

[capture-list]: explicit capture; the capture-list is the list of names of local variables to be captured (i.e., stored in the object) by reference or by value. Variables with names preceded by & are captured by reference. Other variables are captured by value. A capture list can also contain this and names followed by ... as elements.

[&, capture-list]: implicitly capture by reference all local variables with names not men- tioned in the list. The capture list can contain this. Listed names cannot be preceded by &. Variables named in the capture list are captured by value.

[=, capture-list]: implicitly capture by value all local variables with names not mentioned in the list. The capture list cannot contain this. The listed names must be preceded by &. Vari- ables named in the capture list are captured by reference.

Note that a local name preceded by & is always captured by reference and a local name not pre- ceded by & is always captured by value. Only capture by reference allows modification of variables in the calling environment.


Lambda expression format

enter image description here

Additional references:

@Dietrich Baumgarten 2019-10-18 09:42:23

Nice explanation. Using range-based for loops, you can avoid lambdas and shorten the code for (int x : v) { if (x % m == 0) os << x << '\n';}

@nobar 2013-03-01 08:08:08

Lambda expressions are typically used to encapsulate algorithms so that they can be passed to another function. However, it is possible to execute a lambda immediately upon definition:

[&](){ ...your code... }(); // immediately executed lambda expression

is functionally equivalent to

{ ...your code... } // simple code block

This makes lambda expressions a powerful tool for refactoring complex functions. You start by wrapping a code section in a lambda function as shown above. The process of explicit parameterization can then be performed gradually with intermediate testing after each step. Once you have the code-block fully parameterized (as demonstrated by the removal of the &), you can move the code to an external location and make it a normal function.

Similarly, you can use lambda expressions to initialize variables based on the result of an algorithm...

int a = []( int b ){ int r=1; while (b>0) r*=b--; return r; }(5); // 5!

As a way of partitioning your program logic, you might even find it useful to pass a lambda expression as an argument to another lambda expression...

[&]( std::function<void()> algorithm ) // wrapper section
   ...your wrapper code...
   ...your wrapper code...
([&]() // algorithm section
   ...your algorithm code...

Lambda expressions also let you create named nested functions, which can be a convenient way of avoiding duplicate logic. Using named lambdas also tends to be a little easier on the eyes (compared to anonymous inline lambdas) when passing a non-trivial function as a parameter to another function. Note: don't forget the semicolon after the closing curly brace.

auto algorithm = [&]( double x, double m, double b ) -> double
   return m*x+b;

int a=algorithm(1,2,3), b=algorithm(4,5,6);

If subsequent profiling reveals significant initialization overhead for the function object, you might choose to rewrite this as a normal function.

@Piotr99 2013-03-01 08:32:38

Have you realized that this question was asked 1.5 years ago and that the last activity was almost 1 year ago? Anyway, you're contributing some interesting ideas I haven't seen before!

@Blacklight Shining 2013-03-02 01:13:48

Thanks for the simultaneous define-and-execute tip! I think it's worth noting that that works in as a contidion for if statements: if ([i]{ for (char j : i) if (!isspace(j)) return false ; return true ; }()) // i is all whitespace, assuming i is an std::string

@nobar 2013-04-13 22:35:59

So the following is a legal expression: [](){}();.

@dan04 2013-05-30 03:28:54

Ugh! Python's (lambda: None)() syntax is so much more legible.

@Mark Lakata 2014-05-01 20:37:17

I don't understand the last point named nested functions. It looks like double algorithm(double x,double m, double b) {return m*x+b;} does the same thing as the lambda example and is shorter and more compatible.

@nobar 2014-05-02 00:52:45

@MarkLakata: A nested function can be declared inside another function, and can even reference the outer function's data if desired.

@Mark Lakata 2014-05-02 16:05:40

@nobar - you're right, I mistyped. This is legal (I tested it this time) main() {{{{((([](){{}}())));}}}}

@Mark Lakata 2014-05-02 16:10:02

@nobar - I suggest editing your algorithm example so that it uses a non empty capture [] to demonstrate why you would actually use a named nested function rather than a plain old static function. Otherwise (to the C++11 beginner such as myself) they look like they provide the same functionality.

@nobar 2014-05-04 00:32:47

@MarkLakata: You can follow the provided link (*) for a deeper explanation (it goes beyond what you have identified). My example demonstrates the basic syntax, but doesn't cover all of the possible use-cases.

@acgtyrant 2015-07-22 10:16:52

I think lambda expression can not have a name until you say that we can assign it to a object, yeah!

@benzeno 2016-04-17 15:13:57

@acgtyrant That object would be a std::function if I am understanding you correctly. You can store any callable object in it and call it later. Functions like for_each will probably have a std::function parameter.

@nobar 2017-07-13 17:44:01

Aside: JavaScript uses this type of construct for purposes of creating a lexical scope. IIFE

@nobar 2018-04-21 23:30:11

@nobar 2018-04-21 23:43:40

JavaScript calls this pattern 'IIFE' (Immediately Invoked Function Expression). I just encountered a couple of sources calling it 'IILE' (Immediately Invoked Lambda Expresssion) when used in C++.

@Jan 2019-03-31 09:04:50

(lambda: None)() or [](){}(); for some reason I like the second. To me, it's more mathematical, less verbose. All this need for comprehension :) tsk

@sergiol 2015-06-27 00:38:18

One problem it solves: Code simpler than lambda for a call in constructor that uses an output parameter function for initializing a const member

You can initialize a const member of your class, with a call to a function that sets its value by giving back its output as an output parameter.

@SirGuy 2016-09-09 02:50:47

This can also be done with a plain function, which is even what the accepted answer to the question you linked to says to do.

@Ted 2015-03-10 22:36:53

A lambda function is an anonymous function that you create in-line. It can capture variables as some have explained, (e.g. but there are some limitations. For example, if there's a callback interface like this,

void apply(void (*f)(int)) {

you can write a function on the spot to use it like the one passed to apply below:

int col=0;
void output() {
    apply([](int data) {
        cout << data << ((++col % 10) ? ' ' : '\n');

But you can't do this:

void output(int n) {
    int col=0;
    apply([&col,n](int data) {
        cout << data << ((++col % 10) ? ' ' : '\n');

because of limitations in the C++11 standard. If you want to use captures, you have to rely on the library and

#include <functional> 

(or some other STL library like algorithm to get it indirectly) and then work with std::function instead of passing normal functions as parameters like this:

#include <functional>
void apply(std::function<void(int)> f) {
void output(int width) {
    int col;
    apply([width,&col](int data) {
        cout << data << ((++col % width) ? ' ' : '\n');

@sp2danny 2015-03-10 23:50:06

the reason is, that a lambda can only convert to a function pointer, if it has no capture. if apply was a template that accepted a functor, it would work

@Ted 2015-03-11 00:29:58

But the problem is that if apply is an existing interface, you may not have the luxury of being able to declare it differently than a plain old function. The standard could have been designed to allow a new instance of a plain old function to be generated each time such a lambda expression is executed, with generated hard-coded references to the captured variables. It seems a lambda function is generated at compile time. There are other consequences as well. e.g., If you declare a static variable, even if you re-evaluate the lambda expression, you don't get a new static variable.

@sp2danny 2015-03-11 00:37:18

function pointer are often meant to be saved, and a lambdas capture can go out of scope. that only capture-less lambdas convert to function-pointers was by design

@Ted 2015-03-11 01:34:26

You still have to pay attention to stack variables being deallocated for the same reason either way. See… The example I wrote with output and apply is written so that if instead function pointers were allowed and used, they would work as well. The col remains allocated until after all of the function calls from apply have finished. How would you rewrite this code to work using the existing apply interface? Would you end up using global or static variables, or some more obscure transformation of the code?

@Ted 2015-03-11 01:49:20

or perhaps you simply mean that lambda expressions are rvalues and therefore temporary, yet the code remains constant (singleton/static) so that it can be called in the future. In that case, perhaps the function should remain allocated as long as its stack-allocated captures remain allocated. Of course it could get messy unwinding it if for example many variations of the function are allocated in a loop.

@pmr 2011-10-02 15:43:48

What is a lambda function?

The C++ concept of a lambda function originates in the lambda calculus and functional programming. A lambda is an unnamed function that is useful (in actual programming, not theory) for short snippets of code that are impossible to reuse and are not worth naming.

In C++ a lambda function is defined like this

[]() { } // barebone lambda

or in all its glory

[]() mutable -> T { } // T is the return type, still lacking throw()

[] is the capture list, () the argument list and {} the function body.

The capture list

The capture list defines what from the outside of the lambda should be available inside the function body and how. It can be either:

  1. a value: [x]
  2. a reference [&x]
  3. any variable currently in scope by reference [&]
  4. same as 3, but by value [=]

You can mix any of the above in a comma separated list [x, &y].

The argument list

The argument list is the same as in any other C++ function.

The function body

The code that will be executed when the lambda is actually called.

Return type deduction

If a lambda has only one return statement, the return type can be omitted and has the implicit type of decltype(return_statement).


If a lambda is marked mutable (e.g. []() mutable { }) it is allowed to mutate the values that have been captured by value.

Use cases

The library defined by the ISO standard benefits heavily from lambdas and raises the usability several bars as now users don't have to clutter their code with small functors in some accessible scope.


In C++14 lambdas have been extended by various proposals.

Initialized Lambda Captures

An element of the capture list can now be initialized with =. This allows renaming of variables and to capture by moving. An example taken from the standard:

int x = 4;
auto y = [&r = x, x = x+1]()->int {
            r += 2;
            return x+2;
         }();  // Updates ::x to 6, and initializes y to 7.

and one taken from Wikipedia showing how to capture with std::move:

auto ptr = std::make_unique<int>(10); // See below for std::make_unique
auto lambda = [ptr = std::move(ptr)] {return *ptr;};

Generic Lambdas

Lambdas can now be generic (auto would be equivalent to T here if T were a type template argument somewhere in the surrounding scope):

auto lambda = [](auto x, auto y) {return x + y;};

Improved Return Type Deduction

C++14 allows deduced return types for every function and does not restrict it to functions of the form return expression;. This is also extended to lambdas.

@Ramakrishnan Kannan 2016-06-09 13:25:45

In your example for initialized lambda captures above, why do you end the lamba function with the ();? This appears like [](){}(); instead of [](){};. Also shouldn't the value of x be 5?

@The Vee 2016-07-14 13:40:13

@RamakrishnanKannan: 1) the () are there to call the lambda right after defining it and give y its return value. The variable y is an integer, not the lambda. 2) No, x=5 is local to the lambda (a capture by value which just happens to have the same name as the outer scope variable x), and then x+2 = 5+2 is returned. The reassignment of the outer variable x happens through the reference r: r = &x; r += 2;, but this happens to the original value of 4.

Related Questions

Sponsored Content

40 Answered Questions

7 Answered Questions

[SOLVED] What is the difference between 'typedef' and 'using' in C++11?

11 Answered Questions

[SOLVED] What does the explicit keyword mean?

13 Answered Questions

[SOLVED] What is a smart pointer and when should I use one?

22 Answered Questions

[SOLVED] What is the "-->" operator in C++?

12 Answered Questions

[SOLVED] What is move semantics?

5 Answered Questions

[SOLVED] What is the copy-and-swap idiom?

4 Answered Questions

[SOLVED] What does T&& (double ampersand) mean in C++11?

7 Answered Questions

8 Answered Questions

[SOLVED] What is The Rule of Three?

Sponsored Content