对于lambda表达式,noexcept和空抛出规范之间有什么区别吗? [英] Is there any difference between noexcept and empty throw specification for an lambda expression?

查看:105
本文介绍了对于lambda表达式,noexcept和空抛出规范之间有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

举个例子:

double values[] {2.5, -3.5, 4.5, -5.5, 6.5, -7.5};
std::vector<double> squares(std::end(values) - std::begin(values));

std::transform(std::begin(values), std::end(values), std::begin(values), std::begin(squares),
    [](double x1, double x2) throw() { return x1 * x2; });

  1. 这在功能上等同于以下内容吗?

  1. Is this functionally equivalent to the following?

[](double x1, double x2) noexcept { return x1 * x2; })

  • 是否有令人信服的原因,为什么我应该用修饰符标记这样的表达式(或类似的基本表达式),或者在这种情况下,最好不要使用它,而只是不要打扰?

  • Is there a convincible reason, why should I mark such expression (or similar basic expresions) with either modifiers or in this case, it is better to leave it and simply don't bother?

    推荐答案

    noexcept和空抛出规范之间是否有任何区别...?

    Is there any difference between noexcept and empty throw specification...?

    是的.

    想到的第一个区别是,如果引发异常,会发生什么?

    The first difference that comes to mind is what happens if an exception is thrown?

    • 对于throw(),将调用std::unexpected() . unexpected的默认处理程序将调用terminate.
    • 对于noexcept,将调用 std::terminate() .
    • In the case of throw(), std::unexpected() is called. The default handler for unexpected will call terminate.
    • In the case of noexcept, std::terminate() is called.

    第二个是动态异常规范已弃用.

    The second is that the dynamic exception specification is deprecated.

    弃用

    noexcept throw()的改进版本,在C ++ 11中已弃用.与throw()不同, noexcept 不会调用std::unexpected,并且可能会也可能不会取消堆栈,这可能使编译器无需运行时即可实现 noexcept throw()的开销.

    noexcept is an improved version of throw(), which is deprecated in C++11. Unlike throw(), noexcept will not call std::unexpected and may or may not unwind the stack, which potentially allows the compiler to implement noexcept without the runtime overhead of throw().


    是否有令人信服的理由,为什么我要用任一修饰符标记这样的表达式(或类似的基本表达式)??

    Is there a convincible reason, why should I mark such expression (or similar basic expresions) with either modifiers...?

    这是一种意图的表达.如果您打算不让lambda抛出,并且认为这样做对程序执行具有致命性,那么可以-您应将lambda标记为noexcept(不推荐使用throw()).

    It is an expression of intent. If you intend that the lambda never throws, and if it does it is deemed fatal to the execution of the program, then yes - you should mark the lambda as noexcept (throw() is deprecated).

    这篇关于对于lambda表达式,noexcept和空抛出规范之间有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆