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++(中文版)
74
3
更时髦的
auto
返回值(参见条款
3
)在
C++14
中也合法:
template<typename E> // C++14
constexpr
auto
toUType(E enumerator) noexcept
{
return static_cast<std::underlying_type_t<E>>(enumerator);
}
无论这个函数模板怎样写就,
toUType
让我们可以用下面的方式来访问元组中的一个
域:
auto val = std::get<
toUType(UserInfoFields::uiEmail)
>(uInfo);
即使这样写,使用限定作用域的枚举型别还是比不限范围的枚举型别打的字要多,可
是,它还是避免了名字空间污染和无意进行的涉及枚举量的隐式型别转换。在众多情
况下,你可能会同意,多打一些字尚算是一种合理的代价,付出这样的代价所获得的
能力,是避免了枚举型别技术带来的陷阱。毕竟在这种技术刚发明出来的时代,最尖
端的数据通信还在用
2400
波特率的调制解调器呢。
要点速记
C++98
风格的枚举型别,现在称为不限范围的枚举型别。
限定作用域的枚举型别仅在枚举型别内可见。它们只能通过强制型别转换
以转换至其他型别。
限定作用域的枚举型别和不限范围的枚举型别都支持底层型别指定。限定
作用域的枚举型别的默认底层型别是
int
,而不限范围的枚举型别没有默认
底层型别。
限定作用域的枚举型别总是可以进行前置声明,而不限范围的枚举型别却
只有在指定了默认底层型别的前提下才可以进行前置声明。 ...
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