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++(中文版)
右值引用
移动语义和完美转发
161
要点速记
如果函数模板形参具备
T&&
型别,并且
T
的型别系推导而来,或如果对象
使用
auto&&
声明其型别,则该形参或对象就是个万能引用。
如果型别声明并不精确地具备
type&&
的形式,或者型别推导并未发生,则
type&&
就代表右值引用。
若采用右值来初始化万能引用,就会得到一个右值引用。若采用左值来初
始化万能引用,就会得到一个左值引用。
条款
25
:针对右值引用实施
std::move
,针对万
能引用实施
std::forward
右值引用仅会绑定到那些可供移动的对象上。如果形参型别为右值引用,则应该清楚
地了解,它绑定的对象可供移动:
class Widget {
Widget(
Widget&&
rhs);
// rhs
确定无疑地会绑定到
//
可以合于移动目的的对象
};
既然如此,你可能会希望把这些对象传递给其他函数时,使用一种允许该函数利用该
对象的右值性的方式。手法就是,把绑定到了这些对象的形参转换为右值。正如条款
23
介绍的那样,这不仅仅是
std::move
能胜任的工作,这更是它被发明出来的动机:
class Widget {
public:
Widget(Widget&& rhs)
// rhs
是个右值引用
: name(
std::move(rhs.name)
),
p(
std::move(rhs.p)
)
{
}
private:
std::string name;
std::shared_ptr<SomeDataStructure> ...
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