CIS 29 - Notes for Thursday, 3/11

Announcements and Reminders

Group Project Presentations

Group 2, 5, 6 request a presentation time for next Thursday


Lambda Expressions/Functions

A lambda expression allows you to write an anonymous function.  This function is used like an inline function.

Example 1

#include <iostream>
using namespace std;

int main()
    auto hand = [](){cout << "Have a nice day\n";};
  • [](){cout << "Have a nice day\n";} is the lambda expression.  This expression returns a function.

  • The returned function is assigned to a variable, hand.  The type of hand is void (*) (), a pointer to a function.

  • [] is the capture list.  In this example, there is nothing to capture.

  • () represent the lambda function's arguments.  In this example there are none.

  • {cout << "Have a nice day\n";} is the body of the lambda function.

  • There is no indicated return value for the lambda function.

Example 16-2 - lambda capture and lambda arguments

Example 16-3 – captures, arguments, and returns

Example 16-4 – lambda and STL algorithms

Example 16-5 – lambda and function templates