Skip to Content
Effective Modern C++(中文版)
book

Effective Modern C++(中文版)

by Scott Meyers
April 2018
Intermediate to advanced
303 pages
4h 54m
Chinese
China Electric Power Press Ltd.
Content preview from Effective Modern C++(中文版)
lambda
表达式
205
闭包类就是实例化闭包的类。每个
lambda
式都会触发编译器生成一个独一无二
的闭包类。而闭包中的语句会变成它的闭包类成员函数的可执行指令。
lambda
式常用于创建闭包并仅将其用作传递给函数的实参。上述对
std::find_if
的调
用就符合这个情形。不过,一般而言,闭包可以复制。所以,对应于单独一个
lambda
式的闭包型别可以有多个闭包。例如,在下面的代码中:
{
int x;
// x
是个局部变量
auto c1 =
// c1
lambda
产生的闭包的副本
[x](int y) { return x * y > 55; };
auto c2 = c1;
// c2
c1
的副本
auto c3 = c2;
// c3
c2
的副本
}
c1
c2
c3
都是同
lambda
式产生的闭包的副本。
在非正式场合,
lambda
式、闭包和闭包类之间的界线大可以模糊一些。但在本章接下
去的条款中搞清楚上面这几个概念哪些存在于编译期(
lambda
式和闭包类),哪些存
在于运行期(闭包),以及它们彼此之间有何联系,则常常是重要的。
条款
31
:避免默认捕获模式
C++11
中有两种默认捕获模式:按引用或按值。按引用的默认捕获模式可能导致空悬
引用,按值的默认捕获模式会忽悠你,好像可以对空悬引用免疫(其实并没有),你,
让你认为你的闭包是独立的(事实上它们可能不是独立的)。
这些就是本条款的纲领性内容了。但如果你本性上更偏向于工程师而不是领导,你就
会不仅要一个骨架,还得有血有肉。所以我们就从默认捕获模式的危害说起吧。 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

流畅的Python(第2版)

流畅的Python(第2版)

Luciano Ramalho
Go语言编程

Go语言编程

威廉·肯尼迪

Publisher Resources

ISBN: 9787519817749