Comments (3)
我重新回去读了一下此小节,是当年写这段内容的时候没有表达得很清楚,例子也确实不够恰当。书中给出的代码段并没有给出适当的例子来说明用 noexcept 修饰后的函数内部如果抛出异常且没有处理,那么外界到底会不会捕获到函数内部的异常?
这里补充一个新例子:
#include <iostream>
void may_throw() {
throw true;
}
void no_throw() noexcept {
may_throw();
return;
}
auto block_no_throw = []() { may_throw(); };
auto block_throw = []() noexcept { may_throw(); };
int main() {
std::cout << std::boolalpha
<< "may_throw() noexcept? " << noexcept(may_throw()) << std::endl
<< "no_throw() noexcept? " << noexcept(no_throw()) << std::endl
<< "block_no_throw() noexcept? " << noexcept(block_no_throw()) << std::endl
<< "block_throw() noexcept? " << noexcept(block_throw()) << std::endl;
return 0;
}
输出结果是:
may_throw() noexcept? false
no_throw() noexcept? true
block_no_throw() noexcept? false
block_throw() noexcept? true
可以看到 block_no_throw
和 block_throw
内部均调用了会抛出异常的函数 may_throw
,却只有被 noexcept 修饰后的 block_throw
在外部才能获得 noexcept 为 true。而对于 no_throw() 这个函数而言,结果也同样符合预期。
from modern-cpp-tutorial.
由新给出的例子进行进一步测试。将 void no_throw() noexcept 中的 noexcept 去除,使其变为 void no_throw(),输出变成了no_throw() noexcept? false,由此可以推断noexcept(fun())的输出只与原函数后是否跟了noexcept相关。同时,在这几个测试函数中加入输出语句,运行结果表明noexcept(fun())并不会对函数进行调用,函数根本就不会抛出异常,也不存在外界是否会对其进行捕获或处理的问题。
from modern-cpp-tutorial.
noexcept 作为运算符时候,会在编译期对表达式进行检查,同时不会对表达式进行直接求值。相反,从 C++17 起,noexcept 运算符为 true 的唯一条件就是表达式的潜在异常集合为空则结果为 true,否则为 false。对于 noexcept 修饰过的函数通过 noexcept 运算符时得到了 false 的结果,这是不可能的。说明的就是:noexcept 在作为指定符时,对异常有阻断作用。如果一个函数没有被 noexcept 修饰,它可能抛出异常也可能不抛出异常,这是不确定的,所以潜在异常集合不空,因此结果始终为 false。
from modern-cpp-tutorial.
Related Issues (20)
- typo of memory order HOT 1
- 漏字 HOT 1
- Book: Incorrect tables header spacing on Regex chapter
- runtime indexing example question HOT 2
- 中文版pdf第一页license的英文名不对
- 2.2 变量及其初始化 中,if初始化的设计并不是来源于Go或者其他语言,而是Bjarne Stroustrup自己的设计 HOT 2
- 中文版第三章部分加粗显示问题
- C - Wikipedia HOT 1
- Possible text error in Chapter 7
- Question about Chapter 3 Generic Lambda HOT 1
- about std::bind HOT 1
- 语句有些繁琐,可否简化表达 HOT 1
- 第6章的习题设置不合理 HOT 1
- 网页端渲染存在问题 HOT 1
- 关于普通重载和override的一个区别 HOT 1
- 第二章 尾返回类型推导 “我们已经提过 auto 不能用于函数形参进行类型推导” HOT 2
- Chapter 2 exercise answer bug HOT 2
- 第八章内容 HOT 1
- 网页加载速度 HOT 1
- P43 || 43页 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from modern-cpp-tutorial.