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++(中文版)
右值引用
移动语义和完美转发
179
通过让
logAndAddImpl
按索引查找对应名字,然后传递给
logAndAdd
(在那里,它会
经由
std::forward
转发到另一个
logAndAddImpl
载版本),就可以避免在两个重载
版本都放入记录日志的代码。
译注
2
在上述设计中,型别
std::false_type
std::true_type
是所谓“标签”,运用
它们的唯一目的在于强制重载决议按我们想要的方向推进。值得注意的是,这些形参
甚至没有名字。它们在运行期不起任何作用,实际上,我们希望编译器能够识别出这
些标签形参并未使用过,从而将它们从程序的执行镜像中优化掉(某些编译器的确会
这样做,至少有时会)。针对
logAndAdd
内的重载实现函数发起的调用把工作“分派”
到正确的重载版本的手法就是创建适当的标签对象。这种设计因而得名
:
标签分派。
它是模板元编程的标准构件,所以你越是深入地查看当下的
C++
库代码,你也就越是
会频繁地看到这种设计。
就目的而言,更重要的不在于标签分派的工作细节,而是它让我们得以将万能引用和
重载加以组合却不会引发条款
26
所描述的问题的能力。分派函数
logAndAdd
接受的
是个不受限制的万能引用形参,但该函数并未重载。实现函数
logAndAddImpl
则实施
了重载,每个重载版本都接受一个万能引用形参,但重载决议却并不仅对这个万能引
用形参有依赖,还对标签有依赖,而标签值则又加以设计以保证可以命中匹配的函数
不会超过一个。这样设计的结果是,只有标签值才决定了调用的是哪个重载版本。万 ...
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