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++(中文版)
微调
273
要点速记
对于可复制的、在移动成本低廉的并且一定会被复制的形参而言,按值传
递可能会和按引用传递的具备相近的效率,并可能生成更少量的目标代码。
经由构造复制形参的成本可能比经由赋值复制形参高出很多。
按值传递肯定会导致切片问题,所以基类型别特别不适用于按值传递。
条款
42
:考虑置入而非插入
如果你有个容器,持有一些,比如说,
std::string
型别的对象吧,那么似乎合
乎逻辑的做法是经由某个插入函数(
insertion function
)来向其添加新元素(比如
insert
push_front
push_back
,又或者对于
std::forward_list
而言的
insert_
after
),而你传递给函数的元素型别将是
std::string
。毕竟,那正是容器持有之物
的型别。
说合乎逻辑倒也确实合乎逻辑,但却不一定合乎事实。考虑这段代码:
std::vector<std::string> vs;
//
持有
std::string
型别对象的容器
vs.push_back(
"xyzzy"
);
//
添加字符串字面量
这里,容器持有的乃是
std::string
型别对象,但是你手头上有的(你实际上想要实
push_back
的)是个字符串字面量,即,一对引号内的一串字符。字符串字面量不
std::string
,也就是说,传递给
push_back
的实参并非容器持有之物的型别。
std::vector
push_back
针对左值
和右值给出了不同的重载版本如下:
template <class ...
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