我听说noexcept关键字更像是'它永远不应该抛出异常',而不是'它没有'.
我不认为使用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;
}
Run Code Online (Sandbox Code Playgroud)
我认为编译器可以判断一个函数的定义是否包含非抛出语句,因此noexcept如果有一个类似的表达式会更容易使用noexcept(auto),但似乎标准中没有这样的东西.
有没有办法简化noexcept表达式?