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++(中文版)
右值引用
移动语义和完美转发
169
条款
26
:避免依万能引用型别进行重载
假定你需要撰写一个函数,取用一个名字作为形参,然后记录下当前日期和时间,再
把该名字添加到一个全局数据结构中。可能你一开始拿出来的函数长得有点像下面这
样:
std::multiset<std::string> names;
//
全局数据结构
void logAndAdd(const std::string& name)
{
auto now =
//
取得当前时间
std::chrono::system_clock::now();
log(now, "logAndAdd");
//
制备日志条目
names.emplace(name);
//
将名字添加到全局数据结构中,
}
//
关于
emplace
的更多信息参见条款
42
这段代码无可厚非,只是效率方面未能尽如人意。考虑以下三种可能的调用语句:
std::string petName("Darla");
logAndAdd(
petName
);
//
传递左值
std::string
logAndAdd(
std::string("Persephone")
);
//
传递右值
std::string
logAndAdd(
"Patty Dog"
);
//
传递字符串字面量
在第一个调用语句中,
logAndAdd
的形参
name
绑定到了变量
petName
。在
logAndAdd
内部,
name
最终被传递给了
names.emplace ...
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