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++(中文版)
右值引用
移动语义和完美转发
193
条款
29
:假定移动操作不存在、成本高、未使用
移动语义可以说在
C++11
的所有语言特性中占据着首要中的首要地位。“移动容器现
在和复制指针一样成本低廉了!”这是你很可能听说过的,类似说法还有“复制临时
对象现在已经如此高效,如果刻意在撰写代码中避免它,就无异于犯了过早优化的禁
忌!”这些情绪化的言辞不难理解。移动语义确实是个重要的语言特性。语言不只是
允许编译器使用成本相对低廉的移动操作来代替昂贵的复制操作,实际上语言会要求
编译器这样做(只要满足适当条件就必须这样做)。调出你的
C++98
版本的代码存根,
然后只消一字不改地使用符合
C++11
标准的编译器和标准库重新编译一遍,叫一声
“变!”,你的软件便应声增速。
移动语义确有此功,所以这个语言特性一传十、十传百,渐渐成了传奇。传奇嘛,你懂的,
一般都是夸大其辞的结果。本条款就是想让你对这个语言特性的期望接上地气。
让我们从为何许多型别不能支持移动语义的观察开始。整个
C++ 98
标准库都已为
C++11
彻底翻修过,目的是为那些的型别移动的可以实现成比复制更快的型别增添移
动操作,而且库组件的实现也已完成修订以充分利用这些移动操作,不过问题在于你
有可能手上的代码存根并未完成修订以充分利用
C++11
的良好特性。若你的应用中的
(或采用的库中的)型别没有为
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