gpt4 book ai didi

c++ - 是否有自动 noexcept 说明符?

转载 作者:IT老高 更新时间:2023-10-28 13:01:36 28 4
gpt4 key购买 nike

我听说 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;
}

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

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

最佳答案

目前没有。然而,有一个关于该主题的提案,它提出了 noexcept(auto) 语法:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4473根据 Botond Ballo 的“旅行报告:2015 年 5 月在 Lenexa 举行的 C++ 标准 session ”https://botondballo.wordpress.com/2015/06/05/trip-report-c-standards-meeting-in-lenexa-may-2015/,该提案的状态是“需要进一步工作”。

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.

关于c++ - 是否有自动 noexcept 说明符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30456801/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com