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++(中文版)
156
5
};
首先要注意,
std::move
只取用一个函数实参(
rhs.s
),而
std::forward
则既须取
用一个函数实参(
rhs.s
),又须取用一个模板型别实参(
std::string
);其次要注意,
传递给
std::forward
的实参型别应当是个非引用型别,因为习惯上它编码的所传递
实参应该是个右值(参见条款
28
)。这两项合起来,就意味着一是
std::move
在调用
时比
std::forward
需要打的字更少;再是省去了需要传递一个型别实参且由它编码的
须是个右值型别的麻烦。同时,采用
std::move
也消除了传递了错误型别的可能性(例
std::string&
,这会导致数据成员
s
被复制构造而非移动构造)。
更为重要的是,使用
std::move
所要传达的意思是无条件地向右值型别的强制型别转
换,而使用
std::forward
则想说明仅仅对绑定到右值的引用实施向右值型别的强制
型别转换。这是两个非常不同的行为。前者是典型地为移动操作做铺垫,而后者仅仅
是传递(转发)一个对象到另一个函数,而在此过程中无论该对象原始型别具备左值
性(
lvalueness
)和右值性(
rvalueness
),都保持原样。这两个行为是如此不同,因
而最好使用两个不同函数(以及函数名字)来区分这两者。
要点速记
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