CS253: Software Development with C++

Spring 2020

Lambda Functions

Show Lecture.LambdaFunctions as a slide show.

CS253 Lambda Functions

Lambda

λ

This is the Greek letter lambda.

In C++, it refers to an anonymous function, an unnamed function, a function literal.

Functions

A function

Here’s a boring ordinary function:

bool odd(int n) { return n & 0b1; }

int main() {
    cout << odd(42) << ' ' << odd(43) << '\n';
}
0 1

0b indicates a binary constant. Sure, I could have written 0b1 as 1, 01, or 0x01, but 0b1 stresses that it’s a bitmask.

Boolean values are displayed as 0 and 1 by default, unless you use cout << boolalpha.

Pointer to a function

bool odd(int n) { return n & 0b1; }

int main() {
    bool (*p)(int) = odd;
    cout << (*p)(42) << ' ' << p(43) << '\n';
}
0 1

auto is your friend

bool odd(int n) { return n & 0b1; }

int main() {
    auto p = odd;
    cout << (*p)(42) << ' ' << p(43) << '\n';
}
0 1

auto sure made that declaration easier!

There must be a better way.

cout << 355/113.0 << '\n';  // easy   
3.14159

int a=355;
float b=113.0;
cout << a/b << '\n';        // tedious
3.14159

Alternate Function Syntax

There are two ways to declare a function return type:

int alpha() { return 42; }
auto beta() -> int { return 66; }

int main() {
    cout << alpha() << ' ' << beta();
}
42 66

The alternate syntax exists to make template programming simpler; it allows us to deduce the return type using the arguments.

Patience—neither of these are lambda functions.

Alternate Function Syntax

If the return type is not given, it is deduced:

int alpha() { return 42; }
auto gamma() { return 88; }

int main() {
    cout << alpha() << ' ' << gamma();
}
42 88

Alternate Function Syntax

However, the return type must be unambiguous:

auto delta(bool flag) {
    if (flag)
        return 5;
    else
        return 6.7;
}

int main() {
    cout << delta(true);
}
c.cc:5: error: inconsistent deduction for auto return type: 'int' and then 
   'double'

λ-expressions

auto p = [](int n) -> bool { return n & 0b1; };
cout << p(42) << ' ' << p(43) << '\n';
0 1

λ-expressions

The return-type may be deduced, if omitted:

auto p = [](int n) { return n & 0b1; };
cout << p(42) << ' ' << p(43) << '\n';
0 1

What is the return-type of the lambda-expression pointed to by p?

  • n is an int, 0b1 is an int, so n & 0b1 is an int.
  • Therefore, the return-type is int.
  • An int works as well as a bool in this context.
    • Before C had bool, it used int as a boolean type.

Generic λ-expressions

Even the arguments can be auto:

auto twice = [](auto v) { return v + v; };

cout << twice(3) << '\n'
     << twice(2.34) << '\n'
     << twice("Jack"s) << '\n'
     << twice('!') << '\n';
6
4.68
JackJack
66

This is more like a templated function, in that it defines a family of functions.

No arguments

If no arguments are needed, the () can be omitted:

auto unique_id = [] { return getpid(); };
cout << unique_id() << '\n';
3604845

Use