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++(中文版)
196
5
然而,通常你已知代码中会使用的型别,也可以肯定它们的特性不会改变(例如,它
们是否支持成本低廉的移动操作)。如果是这样的情形,你就不需要前面那些假定。
你可以直接查阅所使用的型别对移动的支持细节。如果涉及的型别能够提供成本低廉
的移动操作,并且是在这些移动操作会被调用的语境中使用对象,则可以放心大胆地
依靠移动语义来将复制操作替换成相对不那么昂贵的对应移动操作。
要点速记
假定移动操作不存在、成本高、未使用。
对于那些型别或对于移动语义的支持情况已知的代码,则无需作以上假定。
条款
30
:熟悉完美转发的失败情形
C++11
的宝箱上最引人注目的语言特性纹章之一,就是完美转发。完美转发,可是
完美的哟!不过,揭开宝箱的表面,你才会发现会有这样的“完美”(理想版)和那
样的“完美”(现实版)。
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

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

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

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

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

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

Go语言编程

威廉·肯尼迪

Publisher Resources

ISBN: 9787519817749