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++(中文版)
263
8
微调
C++
中的每一项通用技术或特性,都会在某些情况下适用,而在另一些情况下则不适
用。一般而言,描述通用技术或特性的适用情况才顺理成章,但本章却反其道而行之,
描述了两种例外情况。通用技术指的是按值传递,而通用特性则是置入(
emplacement
)。
欲作出是否采用它们的决定,需面对诸多影响因素,而我能够提供的最佳建议则是需
要考虑它们其实是有适用场景的。话虽如此,这两者可都是高效现代
C++
程序设计的
重要角色,下面的条款为在评价它们是否适用你的软件这方面提供了参考信息。
条款
41
:针对可复制的形参,在移动成本低并且
一定会被复制的前提下,考虑将其按值传递
有些函数的形参本来就是打算拿来复制的。
1
例如,成员函数
addName
可能会将其
形参复制入其私有容器。为效率计,这样的函数应该针对左值实参实施复制,而针对
右值实参实施移动:
class Widget {
public:
void addName(
const std::string&
newName)
//
接受左值
{ names.push_back(newName); }
//
对其实施复制
void addName(
std::string&&
newName)
//
接受右值
{ names.push_back(std::move(newName)); }
//
对其实施移动
//
关于
std::move
的用法,参见条款
25
1
本条款中,针对形参实施的“复制”,意思是将其用作复制或移动操作的源。回忆在本书 ...
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