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++(中文版)
右值引用
移动语义和完美转发
151
于“
type&&
”的天性多义感觉轻松。你还会懂得移动操作为何有如此丰富多样的行为
表现形式。所有这些碎片化的知识都会逐一归位。到了那一刹那,你会回见初心,因
为你会再一次地发现移动语义、完美转发和右值引用显得那样的名副其实。但这一次,
这种感觉将维持不变。
在阅读本章中的条款时,一定要把这一点铭记在心:形参总是左值,即使其型别是右
值引用。即,给定函数形如:
void f(
Widget&& w
);
形参
w
是个左值。即使它的型别是个指涉到
Widget
型别对象的右值引用(如果你对
此感觉意外,请参阅本书第
1
章就写着的关于左值和右值的概述)。
条款
23
:理解
std::move
std::forward
从讲述
std::move
std::forward
不做什么来开始这一章,不无裨益。须知,
std::move
并不进行任何移动,
std::forward
不进行任何转发。
译注
1
这两者在运行
期都无所作为。它们不会生成任何可执行代码,连一个字节都不会生成。
std::move
std::forward
都是仅仅执行强制型别转换的函数(其实是函数模板)。
std::move
无条件地将实参强制转换成右值,而
std::forward
则仅在某个特定条件满
足时才执行同一个强制转换。就是这样。虽然这样的解释可能会引发一堆新的问题,
但是,从根本意义上说,故事讲到这里就全部讲完了。
要把故事讲得更具体一些的话,请看看下面这个
C++11
std::move
示例实现。它
不完全符合标准的所有细节,但已经非常接近了: ...
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