lambda 表达式的 noexcept 和空抛规范之间有什么区别吗?
Is there any difference between noexcept and empty throw specification for an lambda expression?
举个例子:
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; });
这在功能上等同于以下内容吗?
[](double x1, double x2) noexcept { return x1 * x2; })
有没有令人信服的理由,为什么我要用修饰符标记这样的表达式(或类似的基本表达式),或者在这种情况下,最好保留它并且根本不打扰?
Is there any difference between noexcept and empty throw specification...?
是的。
首先想到的区别是如果抛出异常会发生什么?
- 在
throw()
的情况下,调用std::unexpected()
。 unexpected
的默认处理程序将调用 terminate
.
- 在
noexcept
的情况下,调用std::terminate()
。
其二是动态异常规范为deprecated.
Deprecates
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()
已弃用)。
是的,它们都可以用于声明不抛出任何异常的函数(包括 lambda),但是 dynamic exception specification has been deprecated in C++11. And noexcept(与 noexcept(true)
相同)和 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()
.
举个例子:
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; });
这在功能上等同于以下内容吗?
[](double x1, double x2) noexcept { return x1 * x2; })
有没有令人信服的理由,为什么我要用修饰符标记这样的表达式(或类似的基本表达式),或者在这种情况下,最好保留它并且根本不打扰?
Is there any difference between noexcept and empty throw specification...?
是的。
首先想到的区别是如果抛出异常会发生什么?
- 在
throw()
的情况下,调用std::unexpected()
。unexpected
的默认处理程序将调用terminate
. - 在
noexcept
的情况下,调用std::terminate()
。
其二是动态异常规范为deprecated.
Deprecates
noexcept
is an improved version ofthrow()
, which is deprecated in C++11. Unlikethrow()
,noexcept
will not callstd::unexpected
and may or may not unwind the stack, which potentially allows the compiler to implementnoexcept
without the runtime overhead ofthrow()
.
Is there a convincible reason, why should I mark such expression (or similar basic expresions) with either modifiers...?
这是一种意图的表达。如果您希望 lambda 永远不会抛出,并且如果它抛出,则认为它对程序的执行是致命的,那么是的 - 您应该将 lambda 标记为 noexcept
(throw()
已弃用)。
是的,它们都可以用于声明不抛出任何异常的函数(包括 lambda),但是 dynamic exception specification has been deprecated in C++11. And noexcept(与 noexcept(true)
相同)和 throw()
不是完全一样:
noexcept
is an improved version ofthrow()
, which is deprecated in C++11. Unlikethrow()
,noexcept
will not callstd::unexpected
and may or may not unwind the stack, which potentially allows the compiler to implementnoexcept
without the runtime overhead ofthrow()
.