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
表达式
225
在前面,我曾提到,对于
std::bind
了无经验的程序员会感觉占位符(例如,
_1
_2
等)
看起来像天书一样。不过,可不仅仅只有占位符的行为如此佶屈聱牙。假设我们有一
个函数用来制作
Widget
型别对象的压缩副本,
enum class CompLevel { Low, Normal, High };
//
压缩等级
Widget compress(const Widget& w,
//
制作
w
CompLevel lev);
//
压缩副本
然后我们想要创建一个函数对象,这样就可以指定特定的
Widget
型别对象
w
的压缩
级别了。运用
std::bind
,可以创建出这么一个对象:
Widget w;
using namespace std::placeholders;
auto compressRateB = std::bind(compress, w, _1);
这里,当我们把
w
传递给
std::bind
时,然后加以存储,以供未来让
compress
调用时
使用。它存储的位置是在对象
compressRateB
内,但它以哪一种方式存储的:按值,
还是按引用呢?这两者是泾渭分明的,因为如果
w
在对
std::bind
的调用动作与对
compressRateB
调用动作之间被修改了,如果采用按引用方式存储,那么存储起来的
w
的值也会随之修改,而如果采用按值存储,则存储起来的
w
值不会改变。
答案揭晓,
w
是按值存储的。
1
可是,了解答案的唯一途径,就是牢记
std::bind ...
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