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++(中文版)
212
6
要点速记
按引用的默认捕获会导致空悬指针问题。
按值的默认捕获极易受空悬指针影响(尤其是
this
),并会误导人们认为
lambda
式是自洽的。
条款
32
:使用初始化捕获将对象移入闭包
有时,按值的捕获和按引用的捕获皆非你所欲。如果你想要把一个只移对象(例如,
std::unique_ptr
std::future
别的对象)放入闭包,
C++11
未提供任何办法做
到此事。如果你有个对象,其复制操作开销昂贵,而移动操作成本低廉(例如,大部
分标准库容器),而你又需要把该对象放入闭包,那么你肯定更愿意移动该对象,而
非复制它。但是,
C++11
中也还是没有让你实现这一点的途径。
但那只是
C++11
C++14
则有云泥之别。它为对象移动入闭包提供了直接支持。如果
你的编译器兼容
C++14
,则只需欢呼雀跃尔后继续阅读本书即可。但如果你还在使用
C++11
编译器,则你仍可欢呼尔后继续阅读本文,因为
C++11
中有近似达成移动捕
获行为的做法。
移动捕获的缺失即使在
C++11
标准被刚接受时,也被视为一种缺憾。最直接的补救措
施本是在
C++14
中添加这一特性,但标准委员会却另辟蹊径。委员们提出了一种全
新的捕获机制,它是如此灵活,按移动的捕获只不过属于该机制能够实现的多种效果
之一罢了。这种新能力称为初始化捕获(
init capture
)。实际上,它可以做到
C++11
的捕获形式能够做到的所有事情,而且还不止如此。初始化捕获不能表示者,则是默
认捕获模式,但是条款
31
解释过,这是你无论何时都应该远离的一种模式(对于将 ...
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

C++程序设计:原理与实践(基础篇)(原书第2版)

C++程序设计:原理与实践(基础篇)(原书第2版)

本贾尼 斯特劳斯特鲁普
C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普
Go语言编程

Go语言编程

威廉·肯尼迪

Publisher Resources

ISBN: 9787519817749