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++(中文版)
220
6
条款
34
:优先选用
lambda
式,而非
std::bind
std::bind
C++98
std::bind1st
std::bind2nd
后继特性,但是,作为一种
非标准特性而言,
std::bind
2005
年就已经是标准库的组成部分了。正是在那时,
标准委员会接受了名称
TR1
的文档,里面就包含了
std::bind
的规格(在
TR1
中,
bind
位于不同的名字空间,所以是
std::tr1::bind
而非
std::bind
,还有一些接口细
节与现在有所不同)。这样的历史意味着,有些开发者已经有了十多年
std::bind
开发经验。如果你是他们中的一员,那你可能不太情愿放弃这么一个运作良好的工具。
这可以理解,但是对于这个特定的情况,改变是有收益的,因为在
C++11
中,相对于
std::bind
lambda
几乎总会是更好的选择。到了
C++14
lambda
不仅是优势变强,
简直已成为不二之选。
该条款假设你熟悉
std::bind
。如果你还不熟悉,那么在继续阅读之前,还是需要建
立一个基本认识。这种认识在任何情况下都是值得的,因为你并不会知道,在哪个时刻,
就会在你需要阅读或维护的代码中遭遇
std::bind
条款
32
一样,我称
std::bind
返回的函数对象为绑定对象。
之所以说优先选用
lambda
式,而非
std::bind
,最主要原因是
lambda
式具备更高的
可读性。举个例子,假设我们有个函数用来设置声音警报:
//
表示时刻的型别
typedef
(语法参见条款
9
using Time ...
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