Git Product home page Git Product logo

Comments (3)

changkun avatar changkun commented on May 21, 2024

我重新回去读了一下此小节,是当年写这段内容的时候没有表达得很清楚,例子也确实不够恰当。书中给出的代码段并没有给出适当的例子来说明用 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_throwblock_throw 内部均调用了会抛出异常的函数 may_throw,却只有被 noexcept 修饰后的 block_throw 在外部才能获得 noexcept 为 true。而对于 no_throw() 这个函数而言,结果也同样符合预期。

from modern-cpp-tutorial.

razerhell avatar razerhell commented on May 21, 2024

由新给出的例子进行进一步测试。将 void no_throw() noexcept 中的 noexcept 去除,使其变为 void no_throw(),输出变成了no_throw() noexcept? false,由此可以推断noexcept(fun())的输出只与原函数后是否跟了noexcept相关。同时,在这几个测试函数中加入输出语句,运行结果表明noexcept(fun())并不会对函数进行调用,函数根本就不会抛出异常,也不存在外界是否会对其进行捕获或处理的问题。

from modern-cpp-tutorial.

changkun avatar changkun commented on May 21, 2024

noexcept 作为运算符时候,会在编译期对表达式进行检查,同时不会对表达式进行直接求值。相反,从 C++17 起,noexcept 运算符为 true 的唯一条件就是表达式的潜在异常集合为空则结果为 true,否则为 false。对于 noexcept 修饰过的函数通过 noexcept 运算符时得到了 false 的结果,这是不可能的。说明的就是:noexcept 在作为指定符时,对异常有阻断作用。如果一个函数没有被 noexcept 修饰,它可能抛出异常也可能不抛出异常,这是不确定的,所以潜在异常集合不空,因此结果始终为 false。

from modern-cpp-tutorial.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.