Top Banner
[](){}(); Or How I Learned To Stop Worrying and Love the Lambda Pat Viafore HSV.cpp Meetup 3/15/2017
19

Lambda Expressions in C++

Mar 22, 2017

Download

Software

Patrick Viafore
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lambda Expressions in C++

[](){}();Or How I Learned To Stop Worrying and Love the

Lambda

Pat ViaforeHSV.cpp Meetup

3/15/2017

Page 2: Lambda Expressions in C++

Lambda Expressions

Page 3: Lambda Expressions in C++

Lambda Expressions

Lambda Calculus

Church-Turing Thesis

Conversion and Reduction

Complicated Mathematics

Functional Programming

Underpinnings of Computer Science

Lisp?

Page 4: Lambda Expressions in C++
Page 5: Lambda Expressions in C++

Anonymous Functions

Page 6: Lambda Expressions in C++

[](){}

Page 7: Lambda Expressions in C++

[](){

}

Page 8: Lambda Expressions in C++

[](){}Function Body

Page 9: Lambda Expressions in C++

void print(std::function<bool()> func){ std::string str = func() ? "true" : "false"; std::cout << str << “\n”;}

auto isFiveGreaterThanThree = [](){ return 5 > 3; };print(isFiveGreaterThanThree);

Page 10: Lambda Expressions in C++

[](){}

ParametersFunction Body

Page 11: Lambda Expressions in C++

void print(int param, std::function<bool(int)> func){ std::string str = func(param) ? "true" : "false"; std::cout << str << “\n”;}

auto isGreaterThanThree = [](int num){ return num > 3; };print(5, isGreaterThanThree);

Page 12: Lambda Expressions in C++

[](){}Capture List Parameters

Function Body

Page 13: Lambda Expressions in C++

void print(int num, std::function<bool(int)> func){ std::string str = func(num) ? "true" : "false"; std::cout << str << “\n”;}

int target = 3;auto isGreaterThan = [target](int n){ return n > target; };target = 6;print(5, isGreaterThan);

Page 14: Lambda Expressions in C++

Capture ListBy Value By Reference

[this]

[a,b]

[=]

[&a,&b]

[&]

Prefer Explicit Captures Over Default Captures

Page 15: Lambda Expressions in C++

class A {public:

int x;A(): x(10) {}std::function<bool()> getLambda(){

return [=](){ return x+2; };}};

int main(){

A* a = new A();auto lambda = a->getLambda();delete a;lambda();

}

This is implicitly this->x. The this parameter is what gets captured, not x.

Page 16: Lambda Expressions in C++

class A {public:

int x;A(): x(10) {}std::function<bool()> getLambda(){

return [copy=x](){ return copy+2; };}};

int main(){

A* a = new A();auto lambda = a->getLambda();delete a;lambda();

}

C++14 gives us init captures, where we can initialize the variables we capture (useful for any non-local captures)

Page 17: Lambda Expressions in C++

Why?

Dependency Injection

Pass a function to inject a dependency

Functional Programming Styles

Mapping and Filtering just got a whole lot easier

Algorithm Libraryauto isSpecial = [](MacAddress& mac){ return MacAddress::ItuAddress == mac; };

any_of(macs.begin(), macs.end(), isSpecial);count_if(macs.begin(), macs.end(), isSpecial);replace_if(macs.begin(), macs.end(), isSpecial, MacAddress::BlankMacAddress);

sort(ips.begin(), ips.end(), [](IpAddressV4& ip1, IpAddressV4& ip2) { return ip1.getNetworkByteOrder() < ip2.getNetworkByteOrder()); });

Page 18: Lambda Expressions in C++

[](){}Capture List Parameters

Function Body

();

Page 19: Lambda Expressions in C++

Twitter: @PatViaforever