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++(中文版)
转向现代
C++
89
条款
14
:只要函数不会发射异常,就为其加上
noexcept
声明
C++98
中,异常规格可谓喜怒无常的野兽。你必须梳理出一个函数可能发射的所
有异常型别,所以,如果函数实现做了改动,那异常规格也难免要修订。而改动异常
规格可能会破坏客户代码,因为调用方可能依赖于原先的异常规格。编译器通常不会
在保持函数实现、异常规格和客户代码的一致性方面提供什么帮助。绝大多数程序员
最终认定,
C++98
中的异常规格还是不要招惹为妙。
而在
C++11
形成过程中,逐渐达成了一个共识,那就是关于函数发射异常这件事,真
正重要的信息是它到底会不会发射。非黑即白,一个函数或者可能发射异常,或它保
证自己不会。这种要么可能要么不可能的泾渭分明,形成了
C++11
异常规格的基础,
也实质上把
C++98
异常规格替换掉了(
C++98
风格的异常规格仍然成立,但已经它
们已经被标为废弃特性了)。在
C++11
中,无条件的
noexcept
就是为了不会发射异
常的函数而准备的。
函数是否要加上如此声明,事关接口设计。函数是否会发射异常这一行为,是客户方
面关注的核心。调用方可以查询函数的
noexcept
状态,而查询结果可能会影响调用代
码的异常安全性或运行效率。这么一来,函数是否带有
noexcept
声明,就和成员函数
是否带有
const
声明是同等重要的信息。当你明明知道一个函数不会发射异常却未给
它加上
noexcept
声明的话,这就是接口规格缺陷。
但是对不会发射异常的函数应用
noexcept
明还有一个动机,那就是它可以让编译器 ...
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