是否有自动 noexcept 说明符?

Is there an automatic noexcept specifier?

我听说 noexcept 关键字更像是 'it should never throw an exception' 而不是“它没有”。

如果我不确定它是否抛出异常,我认为使用 noexcept 关键字不是很好,但 noexcept 关键字有时与性能相关,例如在移动中构造函数。

所以我尝试使用 noexcept 限定符,但是如果它在定义中有多个语句并且它变成一种复制和粘贴的东西会变得更难。

template <class T>
void f(T&& t)
    noexcept(noexcept(statement_1) &&
             noexcept(statement_2) &&
             noexcept(statement_3) &&
             noexcept(statement_4) &&
             noexcept(statement_5))
{
    statement_1;
    statement_2;
    statement_3;
    statement_4;
    statement_5;
}

我认为编译器可以判断函数的定义是否由非抛出语句组成,因此如果有 noexcept(auto) 这样的表达式,使用 noexcept 会更容易,但它好像标准里没有这个东西

有什么方法可以简化 noexcept 表达式吗?

目前有 none。但是,有一个关于该主题的提案,其中提出 noexcept(auto) 语法:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4473 根据 Botond Ballo 的 "Trip Report: C++ Standards Meeting in Lenexa, May 2015" https://botondballo.wordpress.com/2015/06/05/trip-report-c-standards-meeting-in-lenexa-may-2015/

,此提案的状态为 "needs further work"

Further Work. The proposal’s direction is promising, but it is either not fleshed out well enough, or there are specific concerns with one or more design points. The author is encouraged to come back with a modified proposal that is more fleshed out and/or addresses the stated concerns.

...

noexcept(auto), which basically means “deduce the noexcept-ness of this function from the noexcept-ness of the functions it calls. Like return type deduction, this requires the body of the function being available in each translation unit that uses the function. It was brought up that, together with the proposal for making exception specifications part of the type system, this would mean that modifying the function’s body could change the function’s type (again similarly to return type deduction), but people weren’t overly concerned about that.