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++(中文版)
智能指针
141
computePriority()
);
而在异常安全的调用语句中,传递的则是个左值:
processWidget(spw, computePriority()); //
实参是个左值
由于
processWidget
std::shared_ptr
形参是按值传递的,从右值出发来构造之仅
仅需要一次移动,而从左值出发去构造之则需要一次复制。对于
std::shared_ptr
而言,
这个区别可能会相当可观,因为复制一个
std::shared_ptr
要求对其引用计数进行一
次原子的递增操作,而移动一个
std::shared_ptr
则不要求对其引用计数进行任何操作。
如果我们希望这段异常安全的代码达到非异常安全的代码同样的性能,就需要对
spw
上实施
std::move
,把它转化成一个右值(参见条款
23
):
processWidget(
std::move(
spw
)
,
//
既有效率,又异常安全
computePriority());
这一点很有意思,也值得了解,但通常也无关紧要,因为你很少会有理由不使用
make
系列函数。而除非有充分的理由不去使用
make
系列函数,否则都应该使用它们。
要点速记
相比于直接使用
new
表达式,
make
系列函数消除了重复代码、改进了异常
安全性,并且对于
std::make_shared
std::allcoated_shared
而言,生
成的目标代码会尺寸更小、速度更快。
不适于使用
make
系列函数的场景包括需要定制删除器,以及期望直接传递 ...
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
Effective STL

Effective STL

Scott Meyers

Publisher Resources

ISBN: 9787519817749