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++(中文版)
150
5
右值引用、移动语义
和完美转发
你在初次接触移动语义和完美转发这些概念的时候,它们显得非常名副其实:
移动语义
使得编译器得以使用不那么昂贵的移动操作,来替换昂贵的复制操作。
同复制构造函数、复制赋值运算符给予人们控制对象复制的具体意义的能力一
样,移动构造函数和移动赋值运算符也给予人们控制对象移动语义的能力。移
动语义也使得创建只移型别对象成为可能,这些型别包括
std::unique_ptr
std::future
std::thread
等。
完美转发
使得人们可以撰写接受任意实参的函数模板,并将其转发到其他函数,
目标函数会接受到与转发函数所接受的完全相同的实参。
右值引用是将这两个风马牛不相及的语言特性胶合起来的底层语言机制,正是它使得
移动语义和完美转发成为了可能。
你和这两个语言特性打的交道越多,就越会发现你对它们的初始印象好像只是冰山一
角。移动语义、完美转发和右值引用的世界比它们乍看上去要微妙得多。比如说,
std::move
并没有移动任何东西。再比如说,完美转发并不完美。还有,移动操作的
成本并不总是比复制低,即使低也不一定像你期望的那样低。并且,在移动操作能够
成立的语境中,却并不一定能够调用到移动操作。形如“
type&&
”的结构,也不总是
表示右值引用。
无论在这些特性上钻研多深,好像始终还有更多未知等待开启。幸运的是,它们深而
有底,本章就会带你们到最底层的基石一游。而到达了这一层后,
C++11
的这个部分
就会豁然开朗。例如,你会理解
std::move
std::forward ...
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

流畅的Python(第2版)

流畅的Python(第2版)

Luciano Ramalho
Go语言编程

Go语言编程

威廉·肯尼迪

Publisher Resources

ISBN: 9787519817749