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++(中文版)
41
2
auto
如果单从概念上看,
auto
真是简单到不能再简单,然而细究起来里面却大有文章。使
auto
可以少打一些字,没错,但好处不止这些:它还能阻止那些由于手动指定型别
带来的潜在错误和性能问题。还有,某些
auto
型别推导结果尽管是按部就班地符合标
准规定的推导算法,然而从程序员的视角来看却是错误的。如果是这样的情况,那就
很有必要知道如何去引导
auto
出正确结果,因为退回手工指定型别的声明,是通常
需要避免的途径。
这个小小的章节讨论了有关
auto
的一切。
条款
5
:优先选用
auto
,而非显式型别声明
啊,下面这个语句多么天真无邪:
int x;
等等,我忘记初始化
x
了,所以它的值是不确定的。不过也不一定,它也许被初始化
为零了。到底怎样全看具体语境。
别烦恼。我们可以再看一个天真无邪的例子,使用迭代器的提领结果来初始化局部变
量:
template<typename It>
// dwim
(做我所想,
do what I mean
)算法
void dwim(It b, It e)
//
应用范围是从
b
e
的所有元素
{
while (b != e) {
typename std::iterator_traits<It>::value_type
currValue = *b;
42
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