没有合适的资源?快使用搜索试试~ 我知道了~
C++ Lambda Story
需积分: 5 3 下载量 112 浏览量
2023-02-18
16:29:01
上传
评论
收藏 1.12MB PDF 举报
温馨提示
试读
157页
This book shows the story of lambda expressions in C++. You’ll learn how to use this powerful feature in a step-by-step manner, slowly digesting the new capabilities and enhancements that come with each revision of the C++ Standard. We’ll start with C++98/03, and then we’ll move on to the latest C++ Standards. • C++98/03 - how to code without lambda support. What was the motivation for the new modern C++ feature? • C++11 - early days. You’ll learn about all the elements of a lambda expression an
资源推荐
资源详情
资源评论
C++ Lambda Story
Everything you need to know about Lambda
Expressions in Modern C++!
Bartłomiej Filipek
This book is for sale at http://leanpub.com/cpplambda
This version was published on 2021-07-25
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean
Publishing process. Lean Publishing is the act of publishing an in-progress ebook using
lightweight tools and many iterations to get reader feedback, pivot until you have the right
book and build traction once you do.
© 2019 - 2021 Bartłomiej Filipek
for Wiola and Mikołaj
Contents
About the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i
Roots Of The Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i
Who This Book is For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ii
How To Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ii
Reader Feedback & Errata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii
Example Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii
Code License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv
Formatting And Special Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv
Syntax Highlighting Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . v
Special Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v
Online Compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vi
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . viii
Revision History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
1. Lambdas in C++98/03 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Callable Objects in C++98/03 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
What is a “Functor”? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Issues with Function Object Class Types . . . . . . . . . . . . . . . . . . . . . . . . 7
Composing With Functional Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Motivation for a New Feature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2. Lambdas in C++11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
The Syntax of Lambda Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
A Few Examples of Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . 12
Core Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Compiler Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
CONTENTS
The Type of a Lambda Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Constructors and Copying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
The Call Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Other Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Captures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Generated Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Capture All or Explicit? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
The mutable Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Invocation Counter - An Example of Captured Variables . . . . . . . . . . . . 27
Capturing Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Capturing Statics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Capturing a Class Member And the this Pointer . . . . . . . . . . . . . . . . . 31
Moveable-only Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Preserving Const . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Capturing a Parameter Pack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Return Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Trailing Return Type Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Conversion to a Function Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
A Tricky Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
IIFE - Immediately Invoked Functional Expression . . . . . . . . . . . . . . . . . . 42
One Note About the Readability . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Inheriting from a Lambda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Storing Lambdas in a Container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3. Lambdas in C++14 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Default Parameters for Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Return Type Deduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Captures With an Initialiser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Improvements for Existing Issues . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Generic Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Variadic Generic Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Perfect Forwarding with Generic Lambdas . . . . . . . . . . . . . . . . . . . . . 63
Deducing the Correct Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Replacing std::bind1st and std::bind2nd with Lambdas . . . . . . . . . . . . . 67
剩余156页未读,继续阅读
资源评论
zhenfanwu
- 粉丝: 1
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功