SlideShare a Scribd company logo
1 of 130
Download to read offline
C++11 Lambda Expressions
    Professional C++ Training




ciere consulting
          Michael Caisse

       http://ciere.com/cppnow12
          follow @consultciere

           Copyright c 2012                       ciere.com
      Michael Caisse   C++11 Lambda Expressions
std::disclaimer


         The example code is for demonstrating a purpose
         Please do not assume styles are considered good practice
         Please, never using std; in your own code
         Please, always use scoping and namespaces properly



 Fine print: while we often think these are reasonable guidelines to
 expect from slides, our lawyer made us add a disclaimer

 Fine print to the fine print: not really, but we sometimes here people say, "but you had using std; in your slides"




                                                                                                                      ciere.com
                                        Michael Caisse        C++11 Lambda Expressions
Intro Parts Storing Exercise Use




            Part I

Lambda Expressions




                                                       ciere.com
   Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Outline



      Introduction
      Expression Parts
      Storing
      Exercise
      Use Cases




                                                                        ciere.com
                    Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Some Motivation - Old School



    vector<int>::const_iterator iter     = cardinal.begin();
    vector<int>::const_iterator iter_end = cardinal.end();

    int total_elements = 1;
    while( iter != iter_end )
    {
       total_elements *= *iter;
       ++iter;
    }




                                                                        ciere.com
                    Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Some Motivation - Functor

  int total_elements = 1;
  for_each( cardinal.begin(), cardinal.end(),
            product<int>(total_elements) );


  template <typename T>
  struct product
  {
     product( T & storage ) : value(storage) {}

       template< typename V>
       void operator()( V & v )
       {
          value *= v;
       }

       T & value;
  };
                                                                           ciere.com
                       Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Some Motivation - Functor

  int total_elements = 1;
  for_each( cardinal.begin(), cardinal.end(),
            product<int>(total_elements) );


  template <typename T>
  struct product
  {
     product( T & storage ) : value(storage) {}

       template< typename V>
       void operator()( V & v )
       {
          value *= v;
       }

       T & value;
  };
                                                                           ciere.com
                       Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Some Motivation - Phoenix




    // Boost.Phoenix
    int total_elements = 1;
    for_each( cardinal.begin(), cardinal.end(),
              phx::ref(total_elements) *= _1 );




                                                                        ciere.com
                    Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Some Motivation - Lambda Expression




  int total_elements = 1;
  for_each( cardinal.begin(), cardinal.end(),
            [&total_elements](int i){total_elements *= i;} );




                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
Some Motivation - Lambda Expression
  Before:

     vector<int>::const_iterator iter     = cardinal.begin();
     vector<int>::const_iterator iter_end = cardinal.end();

     int total_elements = 1;
     while( iter != iter_end )
     {
        total_elements *= *iter;
        ++iter;
     }




  After:

  int total_elements = 1;
  for_each( cardinal.begin(), cardinal.end(),
            [&total_elements](int i){total_elements *= i;} );
Functors / Lambda comparison

  struct mod
  {
     mod(int m) : modulus(m) {}
     int operator()(int v){ return v % modulus; }
     int modulus;
  };

  int my_mod = 8;
  transform( in.begin(), in.end(), out.begin(),
             mod(my_mod) );


  int my_mod = 8;
  transform( in.begin(), in.end(), out.begin(),
             [my_mod](int v) ->int
             { return v % my_mod; } );
Functors / Lambda comparison

  struct mod
  {
     mod(int m) : modulus(m) {}
     int operator()(int v){ return v % modulus; }
     int modulus;
  };

  int my_mod = 8;
  transform( in.begin(), in.end(), out.begin(),
             mod(my_mod) );



  int my_mod = 8;
  transform( in.begin(), in.end(), out.begin(),
             [my_mod](int v) ->int
             { return v % my_mod; } );
Functors / Lambda comparison

  struct mod
  {
     mod(int m) : modulus(m) {}
     int operator()(int v){ return v % modulus; }
     int modulus;
  };

  int my_mod = 8;
  transform( in.begin(), in.end(), out.begin(),
             mod(my_mod) );



  int my_mod = 8;
  transform( in.begin(), in.end(), out.begin(),
             [my_mod](int v) ->int
             { return v % my_mod; } );
Functors / Lambda comparison

  struct mod
  {
     mod(int m) : modulus(m) {}
     int operator()(int v){ return v % modulus; }
     int modulus;
  };

  int my_mod = 8;
  transform( in.begin(), in.end(), out.begin(),
             mod(my_mod) );



  int my_mod = 8;
  transform( in.begin(), in.end(), out.begin(),
             [my_mod](int v) ->int
             { return v % my_mod; } );
Functors / Lambda comparison

  struct mod
  {
     mod(int m) : modulus(m) {}
     int operator()(int v){ return v % modulus; }
     int modulus;
  };

  int my_mod = 8;
  transform( in.begin(), in.end(), out.begin(),
             mod(my_mod) );



  int my_mod = 8;
  transform( in.begin(), in.end(), out.begin(),
             [my_mod](int v) ->int
             { return v % my_mod; } );
Intro Parts Storing Exercise Use

Outline



      Introduction
      Expression Parts
      Storing
      Exercise
      Use Cases




                                                                        ciere.com
                    Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Lambda Expression Parts - Introduction




  [my_mod] (int v_)              ->int {return v_ % my_mod;}
    introducer   parameters    return type                 statement
      capture




                                                                             ciere.com
                         Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Lambda Expression Parts - Introduction




  [my_mod] (int v_)              ->int {return v_ % my_mod;}
    introducer   parameters    return type                 statement
      capture




                                                                             ciere.com
                         Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Lambda Expression Parts - Introduction




  [my_mod] (int v_)              ->int {return v_ % my_mod;}
    introducer   parameters    return type                 statement
     capture




                                                                             ciere.com
                         Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Lambda Expression Parts - Introduction




  [my_mod] (int v_)              ->int {return v_ % my_mod;}
    introducer   parameters    return type                 statement
      capture




                                                                             ciere.com
                         Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Lambda Expression Parts - Introduction




  [my_mod] (int v_)              ->int {return v_ % my_mod;}
    introducer   parameters    return type                 statement
      capture




                                                                             ciere.com
                         Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Lambda Expression Parts - Introduction




  [my_mod] (int v_)              ->int {return v_ % my_mod;}
    introducer   parameters    return type                 statement
      capture




                                                                             ciere.com
                         Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Lambda Expression Parts - Introduction




   [my_mod] (int v_) ->int {return v_ % my_mod;}
    introducer   declarator                         statement




                                                                       ciere.com
                   Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Lambda Expression Parts - Introduction



    [my_mod](int v_)->int{return v_ % my_mod;}
                           lambda expression

                                 ⇓
                           closure object

     Evaluation of the expression results in a temporary called a
     closure object
     A closure object is unnamed
     A closure object behaves like a function object



                                                                          ciere.com
                      Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Lambda Expression Parts - Introduction



    [my_mod](int v_)->int{return v_ % my_mod;}
                           lambda expression

                                 ⇓
                           closure object

     Evaluation of the expression results in a temporary called a
     closure object
     A closure object is unnamed
     A closure object behaves like a function object



                                                                          ciere.com
                      Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – introducer




       [](){ cout << "foo" << endl; }




                                                                    ciere.com
                Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – introducer




         []         ()        {cout << "foo" <<endl;}
      introducer parameters                  statement




     We start off a lambda expression with the introducer




                                                                           ciere.com
                       Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – introducer




          [](){cout << "foo" <<endl;}
                           ⇓
                     closure object




                                                                    ciere.com
                Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – introducer



  How can we call this nullary function object-like temporary?

        [](){ cout << "foo" << endl; } ();



  Output
  foo




                                                                           ciere.com
                       Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – introducer



  How can we call this nullary function object-like temporary?

        [](){ cout << "foo" << endl; } ();



  Output
  foo




                                                                           ciere.com
                       Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – parameter




  [](int v){cout << v << "*6=" << v*6 << endl;} (7);



  Output
  7*6=42




                                                                     ciere.com
                 Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – parameter




  [](int v){cout << v << "*6=" << v*6 << endl;} (7);



  Output
  7*6=42




                                                                     ciere.com
                 Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – parameter



  int i = 7;

  [](int & v){ v *= 6; } (i);

  cout << "the correct value is: " << i << endl;



  Output
  the correct value is:           42



                                                                      ciere.com
                 Michael Caisse    C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – parameter



  int i = 7;

  [](int & v){ v *= 6; } (i);

  cout << "the correct value is: " << i << endl;



  Output
  the correct value is:           42



                                                                      ciere.com
                 Michael Caisse    C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – parameter



  int j = 7;

  [](int const & v){ v *= 6; } (j);

  cout << "the correct value is: " << j << endl;



  Compile error
  error: assignment of read-only reference ’v’




                                                                     ciere.com
                 Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – parameter



  int j = 7;

  [](int const & v){ v *= 6; } (j);

  cout << "the correct value is: " << j << endl;



  Compile error
  error: assignment of read-only reference ’v’




                                                                     ciere.com
                 Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – parameter



  int j = 7;

  [](int v)
  {v *= 6; cout << "v: " << v << endl;} (j);



  Output
  v: 42




                                                                     ciere.com
                 Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – parameter



  int j = 7;

  [](int v)
  {v *= 6; cout << "v: " << v << endl;} (j);



  Output
  v: 42




                                                                     ciere.com
                 Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – parameter


  int j = 7;

  [](int & v, int j){ v *= j; } (j,6);

  cout << "j: " << j << endl;


  Notice that the lambda’s parameters do not affect the
  namespace.
  Output
  j: 42


                                                                           ciere.com
                       Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – parameter


  int j = 7;

  [](int & v, int j){ v *= j; } (j,6);

  cout << "j: " << j << endl;


  Notice that the lambda’s parameters do not affect the
  namespace.
  Output
  j: 42


                                                                           ciere.com
                       Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – parameter



          [](){ cout << "foo" << endl; } ();

                               same as

           []{ cout << "foo" << endl; } ();


   Lambda expression without a declarator acts as if it were ()




                                                                          ciere.com
                      Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – parameter



          [](){ cout << "foo" << endl; } ();

                               same as

           []{ cout << "foo" << endl; } ();


   Lambda expression without a declarator acts as if it were ()




                                                                          ciere.com
                      Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – capture

  We commonly want to capture state or access values outside
  our function objects.

  With a function object we use the constructor to populate state.
  struct mod
  {
     mod(int m_ ) : modulus( m_ ) {}
     int operator()(int v_){ return v_ % modulus; }
     int modulus;
  };


  int my_mod = 8;
  transform( in.begin(), in.end(), out.begin(),
             mod(my_mod) );


                                                                           ciere.com
                       Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – capture


  Lambda expressions provide an optional capture.

  [my_mod](int v_) ->int { return v_ % my_mod; }


  We can capture by:
      Default all by reference
      Default all by value
      List of specific identifer(s) by value or reference and/or this
      Default and specific identifiers and/or this



                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – capture


  Lambda expressions provide an optional capture.

                [&](){ ... }


  We can capture by:
      Default all by reference
      Default all by value
      List of specific identifer(s) by value or reference and/or this
      Default and specific identifiers and/or this



                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – capture


  Lambda expressions provide an optional capture.

                [=](){ ... }


  We can capture by:
      Default all by reference
      Default all by value
      List of specific identifer(s) by value or reference and/or this
      Default and specific identifiers and/or this



                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – capture


  Lambda expressions provide an optional capture.

                [identifier](){ ... }


  We can capture by:
      Default all by reference
      Default all by value
      List of specific identifer(s) by value or reference and/or this
      Default and specific identifiers and/or this



                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – capture


  Lambda expressions provide an optional capture.

                [&identifier](){ ... }


  We can capture by:
      Default all by reference
      Default all by value
      List of specific identifer(s) by value or reference and/or this
      Default and specific identifiers and/or this



                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – capture


  Lambda expressions provide an optional capture.

                [foo,&bar,gorp](){ ... }


  We can capture by:
      Default all by reference
      Default all by value
      List of specific identifer(s) by value or reference and/or this
      Default and specific identifiers and/or this



                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – capture


  Lambda expressions provide an optional capture.

                [&,identifier](){ ... }


  We can capture by:
      Default all by reference
      Default all by value
      List of specific identifer(s) by value or reference and/or this
      Default and specific identifiers and/or this



                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – capture


  Lambda expressions provide an optional capture.

                [=,&identifier](){ ... }


  We can capture by:
      Default all by reference
      Default all by value
      List of specific identifer(s) by value or reference and/or this
      Default and specific identifiers and/or this



                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – capture




  Capture default all by reference

  int total_elements = 1;
  for_each( cardinal.begin(), cardinal.end(),
            [&](int i){ total_elements *= i; } );




                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – capture

  template< typename T >
  void fill( vector<int> & v, T done )
  {
     int i = 0;
     while( !done() )
     {
        v.push_back( i++ );
     }
  }
  vector<int> stuff;
  fill( stuff,
        [&]{ return stuff.size() >= 8; } );



  Output
  0 1 2 3 4 5 6 7
                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[&]()->rt{...} – capture

  template< typename T >
  void fill( vector<int> & v, T done )
  {
     int i = 0;
     while( !done() )
     {
        v.push_back( i++ );
     }
  }
  vector<int> stuff;
  fill( stuff,
        [&]{ return stuff.size() >= 8; } );



  Output
  0 1 2 3 4 5 6 7
                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
[&]()->rt{...} – capture
  template< typename T >
  void fill( vector<int> & v, T done )
  {
     int i = 0;
     while( !done() )
     {
        v.push_back( i++ );
     }
  }
  vector<int> stuff;
  fill( stuff,
        [&]{ int sum=0;
             for_each( stuff.begin(), stuff.end(),
                       [&](int i){ sum += i; } );
             return sum >= 10;
           }
     );


  Output
  0 1 2 3 4
[&]()->rt{...} – capture
  template< typename T >
  void fill( vector<int> & v, T done )
  {
     int i = 0;
     while( !done() )
     {
        v.push_back( i++ );
     }
  }
  vector<int> stuff;
  fill( stuff,
        [&]{ int sum=0;
             for_each( stuff.begin(), stuff.end(),
                       [&](int i){ sum += i; } );
             return sum >= 10;
           }
     );


  Output
  0 1 2 3 4
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture




  Capture default all by value

     int my_mod = 8;
     transform( in.begin(), in.end(), out.begin(),
                [=](int v){ return v % my_mod; } );




                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture



  Where is the value captured?

     int v = 42;
     auto func = [=]{ cout << v << endl; };
     v = 8;
     func();




                                                                          ciere.com
                      Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture


  Where is the value captured?

     int v = 42;
     auto func = [=]{ cout << v << endl; };
     v = 8;
     func();


  At the time of evaluation

  Output
  42



                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture



    int i = 10;
    auto two_i = [=]{ i *= 2; return i; };
    cout << "2i:" << two_i() << " i:" << i << endl;




  Compile error
  error: assignment of member
  ’capture_test()::<lambda()>::i’ in read-only
  object



                                                                        ciere.com
                    Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture



    int i = 10;
    auto two_i = [=]{ i *= 2; return i; };
    cout << "2i:" << two_i() << " i:" << i << endl;




  Compile error
  error: assignment of member
  ’capture_test()::<lambda()>::i’ in read-only
  object



                                                                        ciere.com
                    Michael Caisse   C++11 Lambda Expressions
[=]()->rt{...} – capture

  Lambda closure objects have a public inline function call
  operator that:
      Matches the parameters of the lambda expression
      Matches the return type of the lambda expression
      Is declared const


  Make mutable:
     int i = 10;
     auto two_i = [=]() mutable { i *= 2; return i; };
     cout << "2i:" << two_i() << " i:" << i << endl;




  Output
  2i:20 i:10
[=]()->rt{...} – capture

  Lambda closure objects have a public inline function call
  operator that:
      Matches the parameters of the lambda expression
      Matches the return type of the lambda expression
      Is declared const


  Make mutable:
     int i = 10;
     auto two_i = [=]() mutable { i *= 2; return i; };
     cout << "2i:" << two_i() << " i:" << i << endl;




  Output
  2i:20 i:10
[=,&identifer]()->rt{...} – capture
 class gorp
 {
    vector<int> values;
    int m_;

 public:
    gorp(int mod) : m_(mod) {}
    gorp& put(int v){ values.push_back(v); return *this; }

      int extras()
      {
         int count = 0;
         for_each( values.begin(), values.end(),
                   [=,&count](int v){ count += v % m_; } );

          return count;
      }
 };
 gorp g(4);
 g.put(3).put(7).put(8);
 cout << "extras: " << g.extras();
Intro Parts Storing Exercise Use

[=,&identifer]()->rt{...} – capture
 Capture default by value and count by reference
 class gorp
 {
   vector<int> values;
   int m_;

 public:
   int extras()
   {
      int count = 0;
      for_each( values.begin(), values.end(),
                [=,&count](int v){ count += v % m_; } );

          return count;
      }
 };
                                                                          ciere.com
                      Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=,&identifer]()->rt{...} – capture
 Capture count by reference, accumulate, return
 class gorp
 {
   vector<int> values;
   int m_;

 public:
   int extras()
   {
      int count = 0;
      for_each( values.begin(), values.end(),
                [=,&count](int v){ count += v % m_; } );

          return count;
      }
 };
                                                                          ciere.com
                      Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=,&identifer]()->rt{...} – capture
 How did we get m_?
 class gorp
 {
   vector<int> values;
   int m_;

 public:
   int extras()
   {
      int count = 0;
      for_each( values.begin(), values.end(),
                [=,&count](int v){ count += v % m_; } );

          return count;
      }
 };
                                                                          ciere.com
                      Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=,&identifer]()->rt{...} – capture
 Implicit capture of this by value
 class gorp
 {
   vector<int> values;
   int m_;

 public:
   int extras()
   {
      int count = 0;
      for_each( values.begin(), values.end(),
                [=,&count](int v){ count += v % m_; } );

          return count;
      }
 };
                                                                          ciere.com
                      Michael Caisse   C++11 Lambda Expressions
[=,&identifer]()->rt{...} – capture
 class gorp
 {
    vector<int> values;
    int m_;

 public:
      int extras()
      {
         int count = 0;
         for_each( values.begin(), values.end(),
                   [=,&count](int v){ count += v % m_; } );

          return count;
      }
 };
 gorp g(4);
 g.put(3).put(7).put(8);
 cout << "extras: " << g.extras();


 extras:      6
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture


  Will this compile? If so, what is the result?

  struct foo
  {
     foo() : i(0) {}
     void amazing(){ [=]{ i=8; }(); }

       int i;
  };
  foo f;
  f.amazing();
  cout << "f.i : " << f.i;




                                                                             ciere.com
                         Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture
  this implicity captured. mutable not required.

  struct foo
  {
     foo() : i(0) {}
     void amazing(){ [=]{ i=8; }(); }

       int i;
  };
  foo f;
  f.amazing();
  cout << "f.i : " << f.i;



  Output
  f.i :     8
                                                                          ciere.com
                      Michael Caisse   C++11 Lambda Expressions
[=,&identifer]()->rt{...} – capture


 Capture restrictions:

     Identifiers must only be listed once
     Default by value, explicit identifiers by reference
     Default by reference, explicit identifiers by value




   [i,j,&z](){...} // ok
   [&a,b](){...}   // ok
   [z,&i,z](){...} // bad, z listed twice
[=,&identifer]()->rt{...} – capture


 Capture restrictions:

     Identifiers must only be listed once
     Default by value, explicit identifiers by reference
     Default by reference, explicit identifiers by value




   [=,&j,&z](){...}       // ok
   [=,this](){...}        // bad, no this with default =
   [=,&i,z](){...}        // bad, z by value
[=,&identifer]()->rt{...} – capture


 Capture restrictions:

     Identifiers must only be listed once
     Default by value, explicit identifiers by reference
     Default by reference, explicit identifiers by value




   [&,j,z](){...}         // ok
   [&,this](){...}        // ok
   [&,i,&z](){...}        // bad, z by reference
[=]()->rt{...} – capture




  Scope of capture:

      Captured entity must be defined or captured in the
      immediate enclosing lambda expression or function
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture



  int i = 8;
  {
     int j = 2;
     auto f = [=]{ cout << i/j; };
     f();
  }




  Output
  4




                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture



  int i = 8;
  {
     int j = 2;
     auto f = [=]{ cout << i/j; };
     f();
  }




  Output
  4




                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture

  int i = 8;
  auto f =
     [=]()
     {
        int j = 2;
        auto m = [=]{ cout << i/j; };
        m();
     };

  f();




  Output
  4

                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture

  int i = 8;
  auto f =
     [=]()
     {
        int j = 2;
        auto m = [=]{ cout << i/j; };
        m();
     };

  f();




  Output
  4

                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture

  int i = 8;
  auto f =
     [i]()
     {
        int j = 2;
        auto m = [=]{ cout << i/j; };
        m();
     };

  f();




  Output
  4

                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture

  int i = 8;
  auto f =
     [i]()
     {
        int j = 2;
        auto m = [=]{ cout << i/j; };
        m();
     };

  f();




  Output
  4

                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture

  int i = 8;
  auto f =
     []()
     {
        int j = 2;
        auto m = [=]{ cout << i/j; };
        m();
     };

  f();




  Compile error
  error: ’i’ is not captured

                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture

  int i = 8;
  auto f =
     []()
     {
        int j = 2;
        auto m = [=]{ cout << i/j; };
        m();
     };

  f();




  Compile error
  error: ’i’ is not captured

                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture
  int i = 8;
  auto f =
     [=]()
     {
        int j = 2;
        auto m = [&]{ i /= j; };
        m();
        cout << "inner: " << i;
     };

  f();
  cout << " outer: " << i;




  Compile error
  error: assignment of read-only location
  ’...()::<lambda()>::<lambda()>::i’
                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture
  int i = 8;
  auto f =
     [=]()
     {
        int j = 2;
        auto m = [&]{ i /= j; };
        m();
        cout << "inner: " << i;
     };

  f();
  cout << " outer: " << i;




  Compile error
  error: assignment of read-only location
  ’...()::<lambda()>::<lambda()>::i’
                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture

  int i = 8;
  auto f =
     [i]() mutable
     {
        int j = 2;
        auto m = [&i,j]()mutable{ i /= j; };
        m();
        cout << "inner: " << i;
     };

  f();
  cout << " outer: " << i;




  Output
  inner:   4 outer:      8
                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture

  int i = 8;
  auto f =
     [i]() mutable
     {
        int j = 2;
        auto m = [&i,j]()mutable{ i /= j; };
        m();
        cout << "inner: " << i;
     };

  f();
  cout << " outer: " << i;




  Output
  inner:   4 outer:      8
                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
[=]()->rt{...} – capture
  int i=1, j=2, k=3;
  auto f =
     [i,&j,&k]() mutable
     {
        auto m =
           [&i,j,&k]() mutable
           {
              i=4; j=5; k=6;
           };

          m();
          cout << i << j << k;
     };

  f();
  cout << " : " << i << j << k;




  Output
  426 :     126
[=]()->rt{...} – capture
  int i=1, j=2, k=3;
  auto f =
     [i,&j,&k]() mutable
     {
        auto m =
           [&i,j,&k]() mutable
           {
              i=4; j=5; k=6;
           };

          m();
          cout << i << j << k;
     };

  f();
  cout << " : " << i << j << k;




  Output
  426 :     126
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture


      Closure object has implicity-declared copy constructor /
      destructor.


  struct trace
  {
    trace() : i(0)                 {    cout   <<   "constructn"; }
    trace(trace const &)           {    cout   <<   "copy constructn"; }
    ~trace()                       {    cout   <<   "destroyn"; }
    trace& operator=(trace&)       {    cout   <<   "assignn"; return *this;}

   int i;
  };



                                                                             ciere.com
                       Michael Caisse     C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture


      Closure object has implicity-declared copy constructor /
      destructor.


  struct trace
  {
    trace() : i(0)                 {    cout   <<   "constructn"; }
    trace(trace const &)           {    cout   <<   "copy constructn"; }
    ~trace()                       {    cout   <<   "destroyn"; }
    trace& operator=(trace&)       {    cout   <<   "assignn"; return *this;}

   int i;
  };



                                                                             ciere.com
                       Michael Caisse     C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture
  Closure object has implicity-declared copy constructor /
  destructor.

  {
      trace t;
      int i = 8;

      // t not used so not captured
      auto m1 = [=](){ return i/2; };
  }




  Output
  construct
  destroy
                                                                           ciere.com
                       Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

[=]()->rt{...} – capture
  Closure object has implicity-declared copy constructor /
  destructor.

  {
      trace t;
      int i = 8;

      // t not used so not captured
      auto m1 = [=](){ return i/2; };
  }




  Output
  construct
  destroy
                                                                           ciere.com
                       Michael Caisse   C++11 Lambda Expressions
[=]()->rt{...} – capture
  {
      trace t;

      // capture t by value
      auto m1 = [=](){ int i=t.i; };

      cout << "-- make copy --" << endl;
      auto m2 = m1;
  }



  Output
  construct
  copy construct
  - make copy -
  copy construct
  destroy
  destroy
  destroy
[=]()->rt{...} – capture
  {
      trace t;

      // capture t by value
      auto m1 = [=](){ int i=t.i; };

      cout << "-- make copy --" << endl;
      auto m2 = m1;
  }



  Output
  construct
  copy construct
  - make copy -
  copy construct
  destroy
  destroy
  destroy
Intro Parts Storing Exercise Use

[&]()->rt{...} – return type

  If the return type is omited from the lambda expression

   and the statement has a return such as:

                { ...      return expression; }

   then it is the type of the returned expression after:
      lvalue-to-rvalue conversion
      array-to-pointer conversion
      function-to-pointer conversion

  Otherwise, the type is     void

                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

lambda<T>




   for_each( your_base.begin(), your_base.end(),
             [&my_base](base_t & b)
             { my_base.push_back( move(b) ); } );

   // all your base are belong to us




           http://cierecloud.com/cppnow/
                                                                       ciere.com
                   Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Outline



      Introduction
      Expression Parts
      Storing
      Exercise
      Use Cases




                                                                        ciere.com
                    Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Storing / Passing Lambda Objects




  Seen two ways so far:

      tempalte<typename T> void foo(T f)
      auto f = []{};




                                                                          ciere.com
                      Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Function pointer

  If the lambda expression has no capture it can be converted to
  a function pointer with the same signature.

  typedef int(*f_type)(int);

  f_type f = [](int i){ return i+20; };

  cout << f(8);




  Output
  28


                                                                           ciere.com
                       Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Function pointer

  If the lambda expression has no capture it can be converted to
  a function pointer with the same signature.

  typedef int(*f_type)(int);

  f_type f = [](int i){ return i+20; };

  cout << f(8);




  Output
  28


                                                                           ciere.com
                       Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

function<R(Args...)>


 Polymorphic wrapper for function objects applies to anything
 that can be called:
     Function pointers
     Member function pointers
     Functors (including closure objects)



 Function declarator syntax

             std::function< R ( A1, A2, A3...) > f;



                                                                          ciere.com
                      Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

function<R(Args...)>


 Polymorphic wrapper for function objects applies to anything
 that can be called:
     Function pointers
     Member function pointers
     Functors (including closure objects)



 Function declarator syntax

             std::function< R ( A1, A2, A3...) > f;



                                                                          ciere.com
                      Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

function<R(Args...)>


 Polymorphic wrapper for function objects applies to anything
 that can be called:
     Function pointers
     Member function pointers
     Functors (including closure objects)



 Function declarator syntax

             std::function< R ( A1, A2, A3...) > f;



                                                                          ciere.com
                      Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

function<R(Args...)>


 Polymorphic wrapper for function objects applies to anything
 that can be called:
     Function pointers
     Member function pointers
     Functors (including closure objects)



 Function declarator syntax

             std::function< R ( A1, A2, A3...) > f;



                                                                          ciere.com
                      Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

function<R(Args...)>




  Type      Old School Define                      std::function
  Free      int(*callback)(int,int)               function< int(int,int) >
  Member    int (object_t::*callback)(int,int)    function< int(int,int) >
  Functor   object_t callback                     function< int(int,int) >




                                                                              ciere.com
                          Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

function<R(Args...)>


 Function pointers


 int my_free_function(std::string s)
 {
    return s.size();
 }
 std::function< int(std::string) > f;
 f = my_free_function;

 int size = f("cierecloud.com/cppnow");




                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

function<R(Args...)>

 Member function pointers


 struct my_struct
 {
    my_struct( std::string const & s) : s_(s) {}
    int size() const { return s_.size(); }
    std::string s_;
 };
 my_struct mine("cierecloud.com/cppnow");
 std::function< int() > f;

 f = std::bind( &my_struct::size, std::ref(mine) );
 int size = f();



                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

function<R(Args...)>
 Functors

 struct my_functor
 {
    my_functor( std::string const & s) : s_(s) {}
    int operator()() const
    {
       return s_.size();
    }

      std::string s_;
 };
 my_functor mine("cierecloud.com/cppnow");
 std::function< int() > f;

 f = std::ref(mine);
 int size = f();
                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

function<R(Args...)>




 Closure Objects


 std::function< int(std::string const &) > f;

 f = [](std::string const & s){ return s.size(); };
 int size = f("cierecloud.com/cppnow");




                                                                        ciere.com
                    Michael Caisse   C++11 Lambda Expressions
Fun with function

  std::function<int(int)> f1;
  std::function<int(int)> f2 =
     [&](int i)
     {
        cout << i << " ";
        if(i>5) { return f1(i-2); }
     };

  f1 =
     [&](int i)
     {
        cout << i << " ";
        return f2(++i);
     };

  f1(10);

  Output
  10 11 9 10 8 9 7 8 6 7 5 6 4 5
Fun with function

  std::function<int(int)> f1;
  std::function<int(int)> f2 =
     [&](int i)
     {
        cout << i << " ";
        if(i>5) { return f1(i-2); }
     };

  f1 =
     [&](int i)
     {
        cout << i << " ";
        return f2(++i);
     };

  f1(10);

  Output
  10 11 9 10 8 9 7 8 6 7 5 6 4 5
More fun with function

  std::function<int(int)> fact;

  fact =
     [&fact](int n)->int
         {
             if(n==0){ return 1; }
             else
             {
                return (n * fact(n-1));
             }
         } ;

  cout << "factorial(4) : " << fact(4) << endl;




  Output
  factorial(4) :      24
More fun with function

  std::function<int(int)> fact;

  fact =
     [&fact](int n)->int
         {
             if(n==0){ return 1; }
             else
             {
                return (n * fact(n-1));
             }
         } ;

  cout << "factorial(4) : " << fact(4) << endl;




  Output
  factorial(4) :      24
Intro Parts Storing Exercise Use

Outline



      Introduction
      Expression Parts
      Storing
      Exercise
      Use Cases




                                                                        ciere.com
                    Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Exercise

     Class that can queue callable "things"
     The callable "thing" takes an int argument
     The callable "thing" returns an int
     The class will have a method:
     int run( int init )
     When run is called:
           Call each of the queued items
           init will be the initial state of the first call
           The result of each call feeds the input of the next call
           The result of final call will be the return value of run



                http://cierecloud.com/cppnow

                                                                             ciere.com
                         Michael Caisse   C++11 Lambda Expressions
Exercise
  #include   <iostream>
  #include   <algorithm>
  #include   <vector>
  #include   <functional>

  struct machine
  {
     template< typename T >
     void add( T f )
     {
        to_do.push_back(f);
     }

       int run( int v )
       {
          std::for_each( to_do.begin(), to_do.end(),
                         [&v]( std::function<int(int)> f )
                         { v = f(v); } );
          return v;
       }

       std::vector< std::function<int(int)> > to_do;
  };

  int foo(int i){ return i+4; }

  int main()
  {
     machine m;
     m.add( [](int i){ return i*3; } );
     m.add( foo );
     m.add( [](int i){ return i/5; } );

       std::cout << "run(7) : " << m.run(7) << std::endl;
       return 1;
Intro Parts Storing Exercise Use

Outline



      Introduction
      Expression Parts
      Storing
      Exercise
      Use Cases




                                                                        ciere.com
                    Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Where can we use them?



  Lambda expression cannot appear in an unevaluated operand.

      typeid
      sizeof
      noexcept
      decltype




                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Some thoughts




  Make Stepanov happy, revisit standard algorithms.




                                                                          ciere.com
                      Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Some thoughts




     Standard alogrithms
     Callbacks
     Runtime policies
     Locality of expression
     std::bind




                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Some thoughts




     Standard alogrithms
     Callbacks
     Runtime policies
     Locality of expression
     std::bind




                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Some thoughts




     Standard alogrithms
     Callbacks
     Runtime policies
     Locality of expression
     std::bind




                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Some thoughts




     Standard alogrithms
     Callbacks
     Runtime policies
     Locality of expression
     std::bind




                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Some thoughts




     Standard alogrithms
     Callbacks
     Runtime policies
     Locality of expression
     std::bind




                                                                            ciere.com
                        Michael Caisse   C++11 Lambda Expressions
Some thoughts

  template< typename T >
  void foo(T f)
  {
     // amazing profit making function
     money_maker( f(8) );
  }

  int bar( std::string v, int m ){ return m * v.size(); }


  std::string profit_item("socks");

  foo( std::bind( bar, profit_item, _1 ) );

  foo( [profit_item](int m){ return bar(profit_item, m);} );


  Cannot do Currying
Some thoughts

  template< typename T >
  void foo(T f)
  {
     // amazing profit making function
     money_maker( f(8) );
  }

  int bar( std::string v, int m ){ return m * v.size(); }


  std::string profit_item("socks");

  foo( std::bind( bar, profit_item, _1 ) );

  foo( [profit_item](int m){ return bar(profit_item, m);} );


  Cannot do Currying
Intro Parts Storing Exercise Use

Your Thoughts




  How are you going to use lambdas?




                                                                         ciere.com
                     Michael Caisse   C++11 Lambda Expressions
Intro Parts Storing Exercise Use

Slides Available




             http://ciere.com/cppnow12/




                                                                       ciere.com
                   Michael Caisse   C++11 Lambda Expressions
Appendix Appendix




                                                ciere.com
    Michael Caisse   C++11 Lambda Expressions

More Related Content

What's hot

Inline assembly language programs in c
Inline assembly language programs in cInline assembly language programs in c
Inline assembly language programs in cTech_MX
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
 
functions in C and types
functions in C and typesfunctions in C and types
functions in C and typesmubashir farooq
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppteShikshak
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++Ilio Catallo
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 
Sentence-to-Code Traceability Recovery with Domain Ontologies
Sentence-to-Code Traceability Recovery with Domain OntologiesSentence-to-Code Traceability Recovery with Domain Ontologies
Sentence-to-Code Traceability Recovery with Domain OntologiesShinpei Hayashi
 
c.p function
c.p functionc.p function
c.p functiongiri5624
 
Detecting Occurrences of Refactoring with Heuristic Search
Detecting Occurrences of Refactoring with Heuristic SearchDetecting Occurrences of Refactoring with Heuristic Search
Detecting Occurrences of Refactoring with Heuristic SearchShinpei Hayashi
 

What's hot (20)

Inline assembly language programs in c
Inline assembly language programs in cInline assembly language programs in c
Inline assembly language programs in c
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
05. inheritance
05. inheritance05. inheritance
05. inheritance
 
Function in C
Function in CFunction in C
Function in C
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lecture5
Lecture5Lecture5
Lecture5
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
functions in C and types
functions in C and typesfunctions in C and types
functions in C and types
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppt
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Sentence-to-Code Traceability Recovery with Domain Ontologies
Sentence-to-Code Traceability Recovery with Domain OntologiesSentence-to-Code Traceability Recovery with Domain Ontologies
Sentence-to-Code Traceability Recovery with Domain Ontologies
 
C functions
C functionsC functions
C functions
 
c.p function
c.p functionc.p function
c.p function
 
Basics of building a blackfin application
Basics of building a blackfin applicationBasics of building a blackfin application
Basics of building a blackfin application
 
Detecting Occurrences of Refactoring with Heuristic Search
Detecting Occurrences of Refactoring with Heuristic SearchDetecting Occurrences of Refactoring with Heuristic Search
Detecting Occurrences of Refactoring with Heuristic Search
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 

Similar to Lambda

Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Geeks Anonymes
 
Part II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationPart II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationWei-Ren Chen
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlabTarun Gehlot
 
Gude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic ServerGude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic ServerApache Traffic Server
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoringkim.mens
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Languagemspline
 
Gentle introduction to modern C++
Gentle introduction to modern C++Gentle introduction to modern C++
Gentle introduction to modern C++Mihai Todor
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Hermann Hueck
 
Функции обратного вызова в C++. Виталий Ткаченко. CoreHard Spring 2019
Функции обратного вызова в C++. Виталий Ткаченко. CoreHard Spring 2019Функции обратного вызова в C++. Виталий Ткаченко. CoreHard Spring 2019
Функции обратного вызова в C++. Виталий Ткаченко. CoreHard Spring 2019corehard_by
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++msharshitha03s
 
Debugging and Profiling C++ Template Metaprograms
Debugging and Profiling C++ Template MetaprogramsDebugging and Profiling C++ Template Metaprograms
Debugging and Profiling C++ Template MetaprogramsPlatonov Sergey
 
Chapter 5 - Modular Programming.pdf
Chapter 5 - Modular Programming.pdfChapter 5 - Modular Programming.pdf
Chapter 5 - Modular Programming.pdfKirubelWondwoson1
 
Chapter 6 - Modular Programming- in C++.pptx
Chapter 6 - Modular Programming- in C++.pptxChapter 6 - Modular Programming- in C++.pptx
Chapter 6 - Modular Programming- in C++.pptxChereLemma2
 

Similar to Lambda (20)

Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
Part II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationPart II: LLVM Intermediate Representation
Part II: LLVM Intermediate Representation
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
The Style of C++ 11
The Style of C++ 11The Style of C++ 11
The Style of C++ 11
 
Presentation_1370675757603
Presentation_1370675757603Presentation_1370675757603
Presentation_1370675757603
 
SCALA - Functional domain
SCALA -  Functional domainSCALA -  Functional domain
SCALA - Functional domain
 
Gude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic ServerGude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic Server
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
 
Gentle introduction to modern C++
Gentle introduction to modern C++Gentle introduction to modern C++
Gentle introduction to modern C++
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
 
Функции обратного вызова в C++. Виталий Ткаченко. CoreHard Spring 2019
Функции обратного вызова в C++. Виталий Ткаченко. CoreHard Spring 2019Функции обратного вызова в C++. Виталий Ткаченко. CoreHard Spring 2019
Функции обратного вызова в C++. Виталий Ткаченко. CoreHard Spring 2019
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
matlab_simulink_for_control082p.pdf
matlab_simulink_for_control082p.pdfmatlab_simulink_for_control082p.pdf
matlab_simulink_for_control082p.pdf
 
Debugging and Profiling C++ Template Metaprograms
Debugging and Profiling C++ Template MetaprogramsDebugging and Profiling C++ Template Metaprograms
Debugging and Profiling C++ Template Metaprograms
 
Chapter 5 - Modular Programming.pdf
Chapter 5 - Modular Programming.pdfChapter 5 - Modular Programming.pdf
Chapter 5 - Modular Programming.pdf
 
C++11
C++11C++11
C++11
 
Chapter 6 - Modular Programming- in C++.pptx
Chapter 6 - Modular Programming- in C++.pptxChapter 6 - Modular Programming- in C++.pptx
Chapter 6 - Modular Programming- in C++.pptx
 

Recently uploaded

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 

Lambda

  • 1. C++11 Lambda Expressions Professional C++ Training ciere consulting Michael Caisse http://ciere.com/cppnow12 follow @consultciere Copyright c 2012 ciere.com Michael Caisse C++11 Lambda Expressions
  • 2. std::disclaimer The example code is for demonstrating a purpose Please do not assume styles are considered good practice Please, never using std; in your own code Please, always use scoping and namespaces properly Fine print: while we often think these are reasonable guidelines to expect from slides, our lawyer made us add a disclaimer Fine print to the fine print: not really, but we sometimes here people say, "but you had using std; in your slides" ciere.com Michael Caisse C++11 Lambda Expressions
  • 3. Intro Parts Storing Exercise Use Part I Lambda Expressions ciere.com Michael Caisse C++11 Lambda Expressions
  • 4. Intro Parts Storing Exercise Use Outline Introduction Expression Parts Storing Exercise Use Cases ciere.com Michael Caisse C++11 Lambda Expressions
  • 5. Intro Parts Storing Exercise Use Some Motivation - Old School vector<int>::const_iterator iter = cardinal.begin(); vector<int>::const_iterator iter_end = cardinal.end(); int total_elements = 1; while( iter != iter_end ) { total_elements *= *iter; ++iter; } ciere.com Michael Caisse C++11 Lambda Expressions
  • 6. Intro Parts Storing Exercise Use Some Motivation - Functor int total_elements = 1; for_each( cardinal.begin(), cardinal.end(), product<int>(total_elements) ); template <typename T> struct product { product( T & storage ) : value(storage) {} template< typename V> void operator()( V & v ) { value *= v; } T & value; }; ciere.com Michael Caisse C++11 Lambda Expressions
  • 7. Intro Parts Storing Exercise Use Some Motivation - Functor int total_elements = 1; for_each( cardinal.begin(), cardinal.end(), product<int>(total_elements) ); template <typename T> struct product { product( T & storage ) : value(storage) {} template< typename V> void operator()( V & v ) { value *= v; } T & value; }; ciere.com Michael Caisse C++11 Lambda Expressions
  • 8. Intro Parts Storing Exercise Use Some Motivation - Phoenix // Boost.Phoenix int total_elements = 1; for_each( cardinal.begin(), cardinal.end(), phx::ref(total_elements) *= _1 ); ciere.com Michael Caisse C++11 Lambda Expressions
  • 9. Intro Parts Storing Exercise Use Some Motivation - Lambda Expression int total_elements = 1; for_each( cardinal.begin(), cardinal.end(), [&total_elements](int i){total_elements *= i;} ); ciere.com Michael Caisse C++11 Lambda Expressions
  • 10. Some Motivation - Lambda Expression Before: vector<int>::const_iterator iter = cardinal.begin(); vector<int>::const_iterator iter_end = cardinal.end(); int total_elements = 1; while( iter != iter_end ) { total_elements *= *iter; ++iter; } After: int total_elements = 1; for_each( cardinal.begin(), cardinal.end(), [&total_elements](int i){total_elements *= i;} );
  • 11. Functors / Lambda comparison struct mod { mod(int m) : modulus(m) {} int operator()(int v){ return v % modulus; } int modulus; }; int my_mod = 8; transform( in.begin(), in.end(), out.begin(), mod(my_mod) ); int my_mod = 8; transform( in.begin(), in.end(), out.begin(), [my_mod](int v) ->int { return v % my_mod; } );
  • 12. Functors / Lambda comparison struct mod { mod(int m) : modulus(m) {} int operator()(int v){ return v % modulus; } int modulus; }; int my_mod = 8; transform( in.begin(), in.end(), out.begin(), mod(my_mod) ); int my_mod = 8; transform( in.begin(), in.end(), out.begin(), [my_mod](int v) ->int { return v % my_mod; } );
  • 13. Functors / Lambda comparison struct mod { mod(int m) : modulus(m) {} int operator()(int v){ return v % modulus; } int modulus; }; int my_mod = 8; transform( in.begin(), in.end(), out.begin(), mod(my_mod) ); int my_mod = 8; transform( in.begin(), in.end(), out.begin(), [my_mod](int v) ->int { return v % my_mod; } );
  • 14. Functors / Lambda comparison struct mod { mod(int m) : modulus(m) {} int operator()(int v){ return v % modulus; } int modulus; }; int my_mod = 8; transform( in.begin(), in.end(), out.begin(), mod(my_mod) ); int my_mod = 8; transform( in.begin(), in.end(), out.begin(), [my_mod](int v) ->int { return v % my_mod; } );
  • 15. Functors / Lambda comparison struct mod { mod(int m) : modulus(m) {} int operator()(int v){ return v % modulus; } int modulus; }; int my_mod = 8; transform( in.begin(), in.end(), out.begin(), mod(my_mod) ); int my_mod = 8; transform( in.begin(), in.end(), out.begin(), [my_mod](int v) ->int { return v % my_mod; } );
  • 16. Intro Parts Storing Exercise Use Outline Introduction Expression Parts Storing Exercise Use Cases ciere.com Michael Caisse C++11 Lambda Expressions
  • 17. Intro Parts Storing Exercise Use Lambda Expression Parts - Introduction [my_mod] (int v_) ->int {return v_ % my_mod;} introducer parameters return type statement capture ciere.com Michael Caisse C++11 Lambda Expressions
  • 18. Intro Parts Storing Exercise Use Lambda Expression Parts - Introduction [my_mod] (int v_) ->int {return v_ % my_mod;} introducer parameters return type statement capture ciere.com Michael Caisse C++11 Lambda Expressions
  • 19. Intro Parts Storing Exercise Use Lambda Expression Parts - Introduction [my_mod] (int v_) ->int {return v_ % my_mod;} introducer parameters return type statement capture ciere.com Michael Caisse C++11 Lambda Expressions
  • 20. Intro Parts Storing Exercise Use Lambda Expression Parts - Introduction [my_mod] (int v_) ->int {return v_ % my_mod;} introducer parameters return type statement capture ciere.com Michael Caisse C++11 Lambda Expressions
  • 21. Intro Parts Storing Exercise Use Lambda Expression Parts - Introduction [my_mod] (int v_) ->int {return v_ % my_mod;} introducer parameters return type statement capture ciere.com Michael Caisse C++11 Lambda Expressions
  • 22. Intro Parts Storing Exercise Use Lambda Expression Parts - Introduction [my_mod] (int v_) ->int {return v_ % my_mod;} introducer parameters return type statement capture ciere.com Michael Caisse C++11 Lambda Expressions
  • 23. Intro Parts Storing Exercise Use Lambda Expression Parts - Introduction [my_mod] (int v_) ->int {return v_ % my_mod;} introducer declarator statement ciere.com Michael Caisse C++11 Lambda Expressions
  • 24. Intro Parts Storing Exercise Use Lambda Expression Parts - Introduction [my_mod](int v_)->int{return v_ % my_mod;} lambda expression ⇓ closure object Evaluation of the expression results in a temporary called a closure object A closure object is unnamed A closure object behaves like a function object ciere.com Michael Caisse C++11 Lambda Expressions
  • 25. Intro Parts Storing Exercise Use Lambda Expression Parts - Introduction [my_mod](int v_)->int{return v_ % my_mod;} lambda expression ⇓ closure object Evaluation of the expression results in a temporary called a closure object A closure object is unnamed A closure object behaves like a function object ciere.com Michael Caisse C++11 Lambda Expressions
  • 26. Intro Parts Storing Exercise Use [&]()->rt{...} – introducer [](){ cout << "foo" << endl; } ciere.com Michael Caisse C++11 Lambda Expressions
  • 27. Intro Parts Storing Exercise Use [&]()->rt{...} – introducer [] () {cout << "foo" <<endl;} introducer parameters statement We start off a lambda expression with the introducer ciere.com Michael Caisse C++11 Lambda Expressions
  • 28. Intro Parts Storing Exercise Use [&]()->rt{...} – introducer [](){cout << "foo" <<endl;} ⇓ closure object ciere.com Michael Caisse C++11 Lambda Expressions
  • 29. Intro Parts Storing Exercise Use [&]()->rt{...} – introducer How can we call this nullary function object-like temporary? [](){ cout << "foo" << endl; } (); Output foo ciere.com Michael Caisse C++11 Lambda Expressions
  • 30. Intro Parts Storing Exercise Use [&]()->rt{...} – introducer How can we call this nullary function object-like temporary? [](){ cout << "foo" << endl; } (); Output foo ciere.com Michael Caisse C++11 Lambda Expressions
  • 31. Intro Parts Storing Exercise Use [&]()->rt{...} – parameter [](int v){cout << v << "*6=" << v*6 << endl;} (7); Output 7*6=42 ciere.com Michael Caisse C++11 Lambda Expressions
  • 32. Intro Parts Storing Exercise Use [&]()->rt{...} – parameter [](int v){cout << v << "*6=" << v*6 << endl;} (7); Output 7*6=42 ciere.com Michael Caisse C++11 Lambda Expressions
  • 33. Intro Parts Storing Exercise Use [&]()->rt{...} – parameter int i = 7; [](int & v){ v *= 6; } (i); cout << "the correct value is: " << i << endl; Output the correct value is: 42 ciere.com Michael Caisse C++11 Lambda Expressions
  • 34. Intro Parts Storing Exercise Use [&]()->rt{...} – parameter int i = 7; [](int & v){ v *= 6; } (i); cout << "the correct value is: " << i << endl; Output the correct value is: 42 ciere.com Michael Caisse C++11 Lambda Expressions
  • 35. Intro Parts Storing Exercise Use [&]()->rt{...} – parameter int j = 7; [](int const & v){ v *= 6; } (j); cout << "the correct value is: " << j << endl; Compile error error: assignment of read-only reference ’v’ ciere.com Michael Caisse C++11 Lambda Expressions
  • 36. Intro Parts Storing Exercise Use [&]()->rt{...} – parameter int j = 7; [](int const & v){ v *= 6; } (j); cout << "the correct value is: " << j << endl; Compile error error: assignment of read-only reference ’v’ ciere.com Michael Caisse C++11 Lambda Expressions
  • 37. Intro Parts Storing Exercise Use [&]()->rt{...} – parameter int j = 7; [](int v) {v *= 6; cout << "v: " << v << endl;} (j); Output v: 42 ciere.com Michael Caisse C++11 Lambda Expressions
  • 38. Intro Parts Storing Exercise Use [&]()->rt{...} – parameter int j = 7; [](int v) {v *= 6; cout << "v: " << v << endl;} (j); Output v: 42 ciere.com Michael Caisse C++11 Lambda Expressions
  • 39. Intro Parts Storing Exercise Use [&]()->rt{...} – parameter int j = 7; [](int & v, int j){ v *= j; } (j,6); cout << "j: " << j << endl; Notice that the lambda’s parameters do not affect the namespace. Output j: 42 ciere.com Michael Caisse C++11 Lambda Expressions
  • 40. Intro Parts Storing Exercise Use [&]()->rt{...} – parameter int j = 7; [](int & v, int j){ v *= j; } (j,6); cout << "j: " << j << endl; Notice that the lambda’s parameters do not affect the namespace. Output j: 42 ciere.com Michael Caisse C++11 Lambda Expressions
  • 41. Intro Parts Storing Exercise Use [&]()->rt{...} – parameter [](){ cout << "foo" << endl; } (); same as []{ cout << "foo" << endl; } (); Lambda expression without a declarator acts as if it were () ciere.com Michael Caisse C++11 Lambda Expressions
  • 42. Intro Parts Storing Exercise Use [&]()->rt{...} – parameter [](){ cout << "foo" << endl; } (); same as []{ cout << "foo" << endl; } (); Lambda expression without a declarator acts as if it were () ciere.com Michael Caisse C++11 Lambda Expressions
  • 43. Intro Parts Storing Exercise Use [&]()->rt{...} – capture We commonly want to capture state or access values outside our function objects. With a function object we use the constructor to populate state. struct mod { mod(int m_ ) : modulus( m_ ) {} int operator()(int v_){ return v_ % modulus; } int modulus; }; int my_mod = 8; transform( in.begin(), in.end(), out.begin(), mod(my_mod) ); ciere.com Michael Caisse C++11 Lambda Expressions
  • 44. Intro Parts Storing Exercise Use [&]()->rt{...} – capture Lambda expressions provide an optional capture. [my_mod](int v_) ->int { return v_ % my_mod; } We can capture by: Default all by reference Default all by value List of specific identifer(s) by value or reference and/or this Default and specific identifiers and/or this ciere.com Michael Caisse C++11 Lambda Expressions
  • 45. Intro Parts Storing Exercise Use [&]()->rt{...} – capture Lambda expressions provide an optional capture. [&](){ ... } We can capture by: Default all by reference Default all by value List of specific identifer(s) by value or reference and/or this Default and specific identifiers and/or this ciere.com Michael Caisse C++11 Lambda Expressions
  • 46. Intro Parts Storing Exercise Use [&]()->rt{...} – capture Lambda expressions provide an optional capture. [=](){ ... } We can capture by: Default all by reference Default all by value List of specific identifer(s) by value or reference and/or this Default and specific identifiers and/or this ciere.com Michael Caisse C++11 Lambda Expressions
  • 47. Intro Parts Storing Exercise Use [&]()->rt{...} – capture Lambda expressions provide an optional capture. [identifier](){ ... } We can capture by: Default all by reference Default all by value List of specific identifer(s) by value or reference and/or this Default and specific identifiers and/or this ciere.com Michael Caisse C++11 Lambda Expressions
  • 48. Intro Parts Storing Exercise Use [&]()->rt{...} – capture Lambda expressions provide an optional capture. [&identifier](){ ... } We can capture by: Default all by reference Default all by value List of specific identifer(s) by value or reference and/or this Default and specific identifiers and/or this ciere.com Michael Caisse C++11 Lambda Expressions
  • 49. Intro Parts Storing Exercise Use [&]()->rt{...} – capture Lambda expressions provide an optional capture. [foo,&bar,gorp](){ ... } We can capture by: Default all by reference Default all by value List of specific identifer(s) by value or reference and/or this Default and specific identifiers and/or this ciere.com Michael Caisse C++11 Lambda Expressions
  • 50. Intro Parts Storing Exercise Use [&]()->rt{...} – capture Lambda expressions provide an optional capture. [&,identifier](){ ... } We can capture by: Default all by reference Default all by value List of specific identifer(s) by value or reference and/or this Default and specific identifiers and/or this ciere.com Michael Caisse C++11 Lambda Expressions
  • 51. Intro Parts Storing Exercise Use [&]()->rt{...} – capture Lambda expressions provide an optional capture. [=,&identifier](){ ... } We can capture by: Default all by reference Default all by value List of specific identifer(s) by value or reference and/or this Default and specific identifiers and/or this ciere.com Michael Caisse C++11 Lambda Expressions
  • 52. Intro Parts Storing Exercise Use [&]()->rt{...} – capture Capture default all by reference int total_elements = 1; for_each( cardinal.begin(), cardinal.end(), [&](int i){ total_elements *= i; } ); ciere.com Michael Caisse C++11 Lambda Expressions
  • 53. Intro Parts Storing Exercise Use [&]()->rt{...} – capture template< typename T > void fill( vector<int> & v, T done ) { int i = 0; while( !done() ) { v.push_back( i++ ); } } vector<int> stuff; fill( stuff, [&]{ return stuff.size() >= 8; } ); Output 0 1 2 3 4 5 6 7 ciere.com Michael Caisse C++11 Lambda Expressions
  • 54. Intro Parts Storing Exercise Use [&]()->rt{...} – capture template< typename T > void fill( vector<int> & v, T done ) { int i = 0; while( !done() ) { v.push_back( i++ ); } } vector<int> stuff; fill( stuff, [&]{ return stuff.size() >= 8; } ); Output 0 1 2 3 4 5 6 7 ciere.com Michael Caisse C++11 Lambda Expressions
  • 55. [&]()->rt{...} – capture template< typename T > void fill( vector<int> & v, T done ) { int i = 0; while( !done() ) { v.push_back( i++ ); } } vector<int> stuff; fill( stuff, [&]{ int sum=0; for_each( stuff.begin(), stuff.end(), [&](int i){ sum += i; } ); return sum >= 10; } ); Output 0 1 2 3 4
  • 56. [&]()->rt{...} – capture template< typename T > void fill( vector<int> & v, T done ) { int i = 0; while( !done() ) { v.push_back( i++ ); } } vector<int> stuff; fill( stuff, [&]{ int sum=0; for_each( stuff.begin(), stuff.end(), [&](int i){ sum += i; } ); return sum >= 10; } ); Output 0 1 2 3 4
  • 57. Intro Parts Storing Exercise Use [=]()->rt{...} – capture Capture default all by value int my_mod = 8; transform( in.begin(), in.end(), out.begin(), [=](int v){ return v % my_mod; } ); ciere.com Michael Caisse C++11 Lambda Expressions
  • 58. Intro Parts Storing Exercise Use [=]()->rt{...} – capture Where is the value captured? int v = 42; auto func = [=]{ cout << v << endl; }; v = 8; func(); ciere.com Michael Caisse C++11 Lambda Expressions
  • 59. Intro Parts Storing Exercise Use [=]()->rt{...} – capture Where is the value captured? int v = 42; auto func = [=]{ cout << v << endl; }; v = 8; func(); At the time of evaluation Output 42 ciere.com Michael Caisse C++11 Lambda Expressions
  • 60. Intro Parts Storing Exercise Use [=]()->rt{...} – capture int i = 10; auto two_i = [=]{ i *= 2; return i; }; cout << "2i:" << two_i() << " i:" << i << endl; Compile error error: assignment of member ’capture_test()::<lambda()>::i’ in read-only object ciere.com Michael Caisse C++11 Lambda Expressions
  • 61. Intro Parts Storing Exercise Use [=]()->rt{...} – capture int i = 10; auto two_i = [=]{ i *= 2; return i; }; cout << "2i:" << two_i() << " i:" << i << endl; Compile error error: assignment of member ’capture_test()::<lambda()>::i’ in read-only object ciere.com Michael Caisse C++11 Lambda Expressions
  • 62. [=]()->rt{...} – capture Lambda closure objects have a public inline function call operator that: Matches the parameters of the lambda expression Matches the return type of the lambda expression Is declared const Make mutable: int i = 10; auto two_i = [=]() mutable { i *= 2; return i; }; cout << "2i:" << two_i() << " i:" << i << endl; Output 2i:20 i:10
  • 63. [=]()->rt{...} – capture Lambda closure objects have a public inline function call operator that: Matches the parameters of the lambda expression Matches the return type of the lambda expression Is declared const Make mutable: int i = 10; auto two_i = [=]() mutable { i *= 2; return i; }; cout << "2i:" << two_i() << " i:" << i << endl; Output 2i:20 i:10
  • 64. [=,&identifer]()->rt{...} – capture class gorp { vector<int> values; int m_; public: gorp(int mod) : m_(mod) {} gorp& put(int v){ values.push_back(v); return *this; } int extras() { int count = 0; for_each( values.begin(), values.end(), [=,&count](int v){ count += v % m_; } ); return count; } }; gorp g(4); g.put(3).put(7).put(8); cout << "extras: " << g.extras();
  • 65. Intro Parts Storing Exercise Use [=,&identifer]()->rt{...} – capture Capture default by value and count by reference class gorp { vector<int> values; int m_; public: int extras() { int count = 0; for_each( values.begin(), values.end(), [=,&count](int v){ count += v % m_; } ); return count; } }; ciere.com Michael Caisse C++11 Lambda Expressions
  • 66. Intro Parts Storing Exercise Use [=,&identifer]()->rt{...} – capture Capture count by reference, accumulate, return class gorp { vector<int> values; int m_; public: int extras() { int count = 0; for_each( values.begin(), values.end(), [=,&count](int v){ count += v % m_; } ); return count; } }; ciere.com Michael Caisse C++11 Lambda Expressions
  • 67. Intro Parts Storing Exercise Use [=,&identifer]()->rt{...} – capture How did we get m_? class gorp { vector<int> values; int m_; public: int extras() { int count = 0; for_each( values.begin(), values.end(), [=,&count](int v){ count += v % m_; } ); return count; } }; ciere.com Michael Caisse C++11 Lambda Expressions
  • 68. Intro Parts Storing Exercise Use [=,&identifer]()->rt{...} – capture Implicit capture of this by value class gorp { vector<int> values; int m_; public: int extras() { int count = 0; for_each( values.begin(), values.end(), [=,&count](int v){ count += v % m_; } ); return count; } }; ciere.com Michael Caisse C++11 Lambda Expressions
  • 69. [=,&identifer]()->rt{...} – capture class gorp { vector<int> values; int m_; public: int extras() { int count = 0; for_each( values.begin(), values.end(), [=,&count](int v){ count += v % m_; } ); return count; } }; gorp g(4); g.put(3).put(7).put(8); cout << "extras: " << g.extras(); extras: 6
  • 70. Intro Parts Storing Exercise Use [=]()->rt{...} – capture Will this compile? If so, what is the result? struct foo { foo() : i(0) {} void amazing(){ [=]{ i=8; }(); } int i; }; foo f; f.amazing(); cout << "f.i : " << f.i; ciere.com Michael Caisse C++11 Lambda Expressions
  • 71. Intro Parts Storing Exercise Use [=]()->rt{...} – capture this implicity captured. mutable not required. struct foo { foo() : i(0) {} void amazing(){ [=]{ i=8; }(); } int i; }; foo f; f.amazing(); cout << "f.i : " << f.i; Output f.i : 8 ciere.com Michael Caisse C++11 Lambda Expressions
  • 72. [=,&identifer]()->rt{...} – capture Capture restrictions: Identifiers must only be listed once Default by value, explicit identifiers by reference Default by reference, explicit identifiers by value [i,j,&z](){...} // ok [&a,b](){...} // ok [z,&i,z](){...} // bad, z listed twice
  • 73. [=,&identifer]()->rt{...} – capture Capture restrictions: Identifiers must only be listed once Default by value, explicit identifiers by reference Default by reference, explicit identifiers by value [=,&j,&z](){...} // ok [=,this](){...} // bad, no this with default = [=,&i,z](){...} // bad, z by value
  • 74. [=,&identifer]()->rt{...} – capture Capture restrictions: Identifiers must only be listed once Default by value, explicit identifiers by reference Default by reference, explicit identifiers by value [&,j,z](){...} // ok [&,this](){...} // ok [&,i,&z](){...} // bad, z by reference
  • 75. [=]()->rt{...} – capture Scope of capture: Captured entity must be defined or captured in the immediate enclosing lambda expression or function
  • 76. Intro Parts Storing Exercise Use [=]()->rt{...} – capture int i = 8; { int j = 2; auto f = [=]{ cout << i/j; }; f(); } Output 4 ciere.com Michael Caisse C++11 Lambda Expressions
  • 77. Intro Parts Storing Exercise Use [=]()->rt{...} – capture int i = 8; { int j = 2; auto f = [=]{ cout << i/j; }; f(); } Output 4 ciere.com Michael Caisse C++11 Lambda Expressions
  • 78. Intro Parts Storing Exercise Use [=]()->rt{...} – capture int i = 8; auto f = [=]() { int j = 2; auto m = [=]{ cout << i/j; }; m(); }; f(); Output 4 ciere.com Michael Caisse C++11 Lambda Expressions
  • 79. Intro Parts Storing Exercise Use [=]()->rt{...} – capture int i = 8; auto f = [=]() { int j = 2; auto m = [=]{ cout << i/j; }; m(); }; f(); Output 4 ciere.com Michael Caisse C++11 Lambda Expressions
  • 80. Intro Parts Storing Exercise Use [=]()->rt{...} – capture int i = 8; auto f = [i]() { int j = 2; auto m = [=]{ cout << i/j; }; m(); }; f(); Output 4 ciere.com Michael Caisse C++11 Lambda Expressions
  • 81. Intro Parts Storing Exercise Use [=]()->rt{...} – capture int i = 8; auto f = [i]() { int j = 2; auto m = [=]{ cout << i/j; }; m(); }; f(); Output 4 ciere.com Michael Caisse C++11 Lambda Expressions
  • 82. Intro Parts Storing Exercise Use [=]()->rt{...} – capture int i = 8; auto f = []() { int j = 2; auto m = [=]{ cout << i/j; }; m(); }; f(); Compile error error: ’i’ is not captured ciere.com Michael Caisse C++11 Lambda Expressions
  • 83. Intro Parts Storing Exercise Use [=]()->rt{...} – capture int i = 8; auto f = []() { int j = 2; auto m = [=]{ cout << i/j; }; m(); }; f(); Compile error error: ’i’ is not captured ciere.com Michael Caisse C++11 Lambda Expressions
  • 84. Intro Parts Storing Exercise Use [=]()->rt{...} – capture int i = 8; auto f = [=]() { int j = 2; auto m = [&]{ i /= j; }; m(); cout << "inner: " << i; }; f(); cout << " outer: " << i; Compile error error: assignment of read-only location ’...()::<lambda()>::<lambda()>::i’ ciere.com Michael Caisse C++11 Lambda Expressions
  • 85. Intro Parts Storing Exercise Use [=]()->rt{...} – capture int i = 8; auto f = [=]() { int j = 2; auto m = [&]{ i /= j; }; m(); cout << "inner: " << i; }; f(); cout << " outer: " << i; Compile error error: assignment of read-only location ’...()::<lambda()>::<lambda()>::i’ ciere.com Michael Caisse C++11 Lambda Expressions
  • 86. Intro Parts Storing Exercise Use [=]()->rt{...} – capture int i = 8; auto f = [i]() mutable { int j = 2; auto m = [&i,j]()mutable{ i /= j; }; m(); cout << "inner: " << i; }; f(); cout << " outer: " << i; Output inner: 4 outer: 8 ciere.com Michael Caisse C++11 Lambda Expressions
  • 87. Intro Parts Storing Exercise Use [=]()->rt{...} – capture int i = 8; auto f = [i]() mutable { int j = 2; auto m = [&i,j]()mutable{ i /= j; }; m(); cout << "inner: " << i; }; f(); cout << " outer: " << i; Output inner: 4 outer: 8 ciere.com Michael Caisse C++11 Lambda Expressions
  • 88. [=]()->rt{...} – capture int i=1, j=2, k=3; auto f = [i,&j,&k]() mutable { auto m = [&i,j,&k]() mutable { i=4; j=5; k=6; }; m(); cout << i << j << k; }; f(); cout << " : " << i << j << k; Output 426 : 126
  • 89. [=]()->rt{...} – capture int i=1, j=2, k=3; auto f = [i,&j,&k]() mutable { auto m = [&i,j,&k]() mutable { i=4; j=5; k=6; }; m(); cout << i << j << k; }; f(); cout << " : " << i << j << k; Output 426 : 126
  • 90. Intro Parts Storing Exercise Use [=]()->rt{...} – capture Closure object has implicity-declared copy constructor / destructor. struct trace { trace() : i(0) { cout << "constructn"; } trace(trace const &) { cout << "copy constructn"; } ~trace() { cout << "destroyn"; } trace& operator=(trace&) { cout << "assignn"; return *this;} int i; }; ciere.com Michael Caisse C++11 Lambda Expressions
  • 91. Intro Parts Storing Exercise Use [=]()->rt{...} – capture Closure object has implicity-declared copy constructor / destructor. struct trace { trace() : i(0) { cout << "constructn"; } trace(trace const &) { cout << "copy constructn"; } ~trace() { cout << "destroyn"; } trace& operator=(trace&) { cout << "assignn"; return *this;} int i; }; ciere.com Michael Caisse C++11 Lambda Expressions
  • 92. Intro Parts Storing Exercise Use [=]()->rt{...} – capture Closure object has implicity-declared copy constructor / destructor. { trace t; int i = 8; // t not used so not captured auto m1 = [=](){ return i/2; }; } Output construct destroy ciere.com Michael Caisse C++11 Lambda Expressions
  • 93. Intro Parts Storing Exercise Use [=]()->rt{...} – capture Closure object has implicity-declared copy constructor / destructor. { trace t; int i = 8; // t not used so not captured auto m1 = [=](){ return i/2; }; } Output construct destroy ciere.com Michael Caisse C++11 Lambda Expressions
  • 94. [=]()->rt{...} – capture { trace t; // capture t by value auto m1 = [=](){ int i=t.i; }; cout << "-- make copy --" << endl; auto m2 = m1; } Output construct copy construct - make copy - copy construct destroy destroy destroy
  • 95. [=]()->rt{...} – capture { trace t; // capture t by value auto m1 = [=](){ int i=t.i; }; cout << "-- make copy --" << endl; auto m2 = m1; } Output construct copy construct - make copy - copy construct destroy destroy destroy
  • 96. Intro Parts Storing Exercise Use [&]()->rt{...} – return type If the return type is omited from the lambda expression and the statement has a return such as: { ... return expression; } then it is the type of the returned expression after: lvalue-to-rvalue conversion array-to-pointer conversion function-to-pointer conversion Otherwise, the type is void ciere.com Michael Caisse C++11 Lambda Expressions
  • 97. Intro Parts Storing Exercise Use lambda<T> for_each( your_base.begin(), your_base.end(), [&my_base](base_t & b) { my_base.push_back( move(b) ); } ); // all your base are belong to us http://cierecloud.com/cppnow/ ciere.com Michael Caisse C++11 Lambda Expressions
  • 98. Intro Parts Storing Exercise Use Outline Introduction Expression Parts Storing Exercise Use Cases ciere.com Michael Caisse C++11 Lambda Expressions
  • 99. Intro Parts Storing Exercise Use Storing / Passing Lambda Objects Seen two ways so far: tempalte<typename T> void foo(T f) auto f = []{}; ciere.com Michael Caisse C++11 Lambda Expressions
  • 100. Intro Parts Storing Exercise Use Function pointer If the lambda expression has no capture it can be converted to a function pointer with the same signature. typedef int(*f_type)(int); f_type f = [](int i){ return i+20; }; cout << f(8); Output 28 ciere.com Michael Caisse C++11 Lambda Expressions
  • 101. Intro Parts Storing Exercise Use Function pointer If the lambda expression has no capture it can be converted to a function pointer with the same signature. typedef int(*f_type)(int); f_type f = [](int i){ return i+20; }; cout << f(8); Output 28 ciere.com Michael Caisse C++11 Lambda Expressions
  • 102. Intro Parts Storing Exercise Use function<R(Args...)> Polymorphic wrapper for function objects applies to anything that can be called: Function pointers Member function pointers Functors (including closure objects) Function declarator syntax std::function< R ( A1, A2, A3...) > f; ciere.com Michael Caisse C++11 Lambda Expressions
  • 103. Intro Parts Storing Exercise Use function<R(Args...)> Polymorphic wrapper for function objects applies to anything that can be called: Function pointers Member function pointers Functors (including closure objects) Function declarator syntax std::function< R ( A1, A2, A3...) > f; ciere.com Michael Caisse C++11 Lambda Expressions
  • 104. Intro Parts Storing Exercise Use function<R(Args...)> Polymorphic wrapper for function objects applies to anything that can be called: Function pointers Member function pointers Functors (including closure objects) Function declarator syntax std::function< R ( A1, A2, A3...) > f; ciere.com Michael Caisse C++11 Lambda Expressions
  • 105. Intro Parts Storing Exercise Use function<R(Args...)> Polymorphic wrapper for function objects applies to anything that can be called: Function pointers Member function pointers Functors (including closure objects) Function declarator syntax std::function< R ( A1, A2, A3...) > f; ciere.com Michael Caisse C++11 Lambda Expressions
  • 106. Intro Parts Storing Exercise Use function<R(Args...)> Type Old School Define std::function Free int(*callback)(int,int) function< int(int,int) > Member int (object_t::*callback)(int,int) function< int(int,int) > Functor object_t callback function< int(int,int) > ciere.com Michael Caisse C++11 Lambda Expressions
  • 107. Intro Parts Storing Exercise Use function<R(Args...)> Function pointers int my_free_function(std::string s) { return s.size(); } std::function< int(std::string) > f; f = my_free_function; int size = f("cierecloud.com/cppnow"); ciere.com Michael Caisse C++11 Lambda Expressions
  • 108. Intro Parts Storing Exercise Use function<R(Args...)> Member function pointers struct my_struct { my_struct( std::string const & s) : s_(s) {} int size() const { return s_.size(); } std::string s_; }; my_struct mine("cierecloud.com/cppnow"); std::function< int() > f; f = std::bind( &my_struct::size, std::ref(mine) ); int size = f(); ciere.com Michael Caisse C++11 Lambda Expressions
  • 109. Intro Parts Storing Exercise Use function<R(Args...)> Functors struct my_functor { my_functor( std::string const & s) : s_(s) {} int operator()() const { return s_.size(); } std::string s_; }; my_functor mine("cierecloud.com/cppnow"); std::function< int() > f; f = std::ref(mine); int size = f(); ciere.com Michael Caisse C++11 Lambda Expressions
  • 110. Intro Parts Storing Exercise Use function<R(Args...)> Closure Objects std::function< int(std::string const &) > f; f = [](std::string const & s){ return s.size(); }; int size = f("cierecloud.com/cppnow"); ciere.com Michael Caisse C++11 Lambda Expressions
  • 111. Fun with function std::function<int(int)> f1; std::function<int(int)> f2 = [&](int i) { cout << i << " "; if(i>5) { return f1(i-2); } }; f1 = [&](int i) { cout << i << " "; return f2(++i); }; f1(10); Output 10 11 9 10 8 9 7 8 6 7 5 6 4 5
  • 112. Fun with function std::function<int(int)> f1; std::function<int(int)> f2 = [&](int i) { cout << i << " "; if(i>5) { return f1(i-2); } }; f1 = [&](int i) { cout << i << " "; return f2(++i); }; f1(10); Output 10 11 9 10 8 9 7 8 6 7 5 6 4 5
  • 113. More fun with function std::function<int(int)> fact; fact = [&fact](int n)->int { if(n==0){ return 1; } else { return (n * fact(n-1)); } } ; cout << "factorial(4) : " << fact(4) << endl; Output factorial(4) : 24
  • 114. More fun with function std::function<int(int)> fact; fact = [&fact](int n)->int { if(n==0){ return 1; } else { return (n * fact(n-1)); } } ; cout << "factorial(4) : " << fact(4) << endl; Output factorial(4) : 24
  • 115. Intro Parts Storing Exercise Use Outline Introduction Expression Parts Storing Exercise Use Cases ciere.com Michael Caisse C++11 Lambda Expressions
  • 116. Intro Parts Storing Exercise Use Exercise Class that can queue callable "things" The callable "thing" takes an int argument The callable "thing" returns an int The class will have a method: int run( int init ) When run is called: Call each of the queued items init will be the initial state of the first call The result of each call feeds the input of the next call The result of final call will be the return value of run http://cierecloud.com/cppnow ciere.com Michael Caisse C++11 Lambda Expressions
  • 117. Exercise #include <iostream> #include <algorithm> #include <vector> #include <functional> struct machine { template< typename T > void add( T f ) { to_do.push_back(f); } int run( int v ) { std::for_each( to_do.begin(), to_do.end(), [&v]( std::function<int(int)> f ) { v = f(v); } ); return v; } std::vector< std::function<int(int)> > to_do; }; int foo(int i){ return i+4; } int main() { machine m; m.add( [](int i){ return i*3; } ); m.add( foo ); m.add( [](int i){ return i/5; } ); std::cout << "run(7) : " << m.run(7) << std::endl; return 1;
  • 118. Intro Parts Storing Exercise Use Outline Introduction Expression Parts Storing Exercise Use Cases ciere.com Michael Caisse C++11 Lambda Expressions
  • 119. Intro Parts Storing Exercise Use Where can we use them? Lambda expression cannot appear in an unevaluated operand. typeid sizeof noexcept decltype ciere.com Michael Caisse C++11 Lambda Expressions
  • 120. Intro Parts Storing Exercise Use Some thoughts Make Stepanov happy, revisit standard algorithms. ciere.com Michael Caisse C++11 Lambda Expressions
  • 121. Intro Parts Storing Exercise Use Some thoughts Standard alogrithms Callbacks Runtime policies Locality of expression std::bind ciere.com Michael Caisse C++11 Lambda Expressions
  • 122. Intro Parts Storing Exercise Use Some thoughts Standard alogrithms Callbacks Runtime policies Locality of expression std::bind ciere.com Michael Caisse C++11 Lambda Expressions
  • 123. Intro Parts Storing Exercise Use Some thoughts Standard alogrithms Callbacks Runtime policies Locality of expression std::bind ciere.com Michael Caisse C++11 Lambda Expressions
  • 124. Intro Parts Storing Exercise Use Some thoughts Standard alogrithms Callbacks Runtime policies Locality of expression std::bind ciere.com Michael Caisse C++11 Lambda Expressions
  • 125. Intro Parts Storing Exercise Use Some thoughts Standard alogrithms Callbacks Runtime policies Locality of expression std::bind ciere.com Michael Caisse C++11 Lambda Expressions
  • 126. Some thoughts template< typename T > void foo(T f) { // amazing profit making function money_maker( f(8) ); } int bar( std::string v, int m ){ return m * v.size(); } std::string profit_item("socks"); foo( std::bind( bar, profit_item, _1 ) ); foo( [profit_item](int m){ return bar(profit_item, m);} ); Cannot do Currying
  • 127. Some thoughts template< typename T > void foo(T f) { // amazing profit making function money_maker( f(8) ); } int bar( std::string v, int m ){ return m * v.size(); } std::string profit_item("socks"); foo( std::bind( bar, profit_item, _1 ) ); foo( [profit_item](int m){ return bar(profit_item, m);} ); Cannot do Currying
  • 128. Intro Parts Storing Exercise Use Your Thoughts How are you going to use lambdas? ciere.com Michael Caisse C++11 Lambda Expressions
  • 129. Intro Parts Storing Exercise Use Slides Available http://ciere.com/cppnow12/ ciere.com Michael Caisse C++11 Lambda Expressions
  • 130. Appendix Appendix ciere.com Michael Caisse C++11 Lambda Expressions