Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ noexcept运算符编译时检查_C++_Noexcept - Fatal编程技术网

C++ noexcept运算符编译时检查

C++ noexcept运算符编译时检查,c++,noexcept,C++,Noexcept,在下面的代码中,我试图对函数使用条件异常规范,但编译失败,尽管如果在函数外部使用,它可以正常工作 void may_throw(); // ERROR: expression must have bool type or be convertible to bool void check () noexcept(may_throw()); int main() { // works just fine! std::cout << noexcept(may_thro

在下面的代码中,我试图对函数使用条件异常规范,但编译失败,尽管如果在函数外部使用,它可以正常工作

void may_throw();

// ERROR: expression must have bool type or be convertible to bool
void check () noexcept(may_throw());

int main()
{
    // works just fine!
    std::cout << noexcept(may_throw());
}
给定无效检查noexceptmay_throw;,需要一个可转换为bool的表达式,而may_throw返回void,无法转换为bool

您应该应用于may_throw并将其指定给noexcept说明符。i、 e

// whether check is declared noexcept depends on if the expression
// may_throw() will throw any exceptions
void check () noexcept(noexcept(may_throw()));
//            ^^^^^^^^          -> specifies whether check could throw exceptions 
//                     ^^^^^^^^ -> performs a compile-time check that returns true if may_throw() is declared to not throw any exceptions, and false if not.
给定无效检查noexceptmay_throw;,需要一个可转换为bool的表达式,而may_throw返回void,无法转换为bool

您应该应用于may_throw并将其指定给noexcept说明符。i、 e

// whether check is declared noexcept depends on if the expression
// may_throw() will throw any exceptions
void check () noexcept(noexcept(may_throw()));
//            ^^^^^^^^          -> specifies whether check could throw exceptions 
//                     ^^^^^^^^ -> performs a compile-time check that returns true if may_throw() is declared to not throw any exceptions, and false if not.

正确的方法是:

noexcept( noexcept( may_throw() ) )
至于原因,第一个noexcept被定义为noexcept说明符。这用于判断函数是否为noexcept。它的形式来自[except.spec]:

第二个是noexcept运算符:noexcept运算符确定是否对其操作数求值,该操作数是未求值的操作数
8.2,可以引发异常18.1。从[expr.unary.noexcept]

执行此操作的正确方法是:

noexcept( noexcept( may_throw() ) )
至于原因,第一个noexcept被定义为noexcept说明符。这用于判断函数是否为noexcept。它的形式来自[except.spec]:

第二个是noexcept运算符:noexcept运算符确定是否对其操作数求值,该操作数是未求值的操作数
8.2,可以引发异常18.1。从[expr.unary.noexcept]

noexcept noexcept可能会抛出进行编辑,您认为不工作是什么意思?我的意思是检查函数将声明为noexcept,但它实际上会抛出,因为它调用了将抛出的ASDF。您的打印检查有缺陷。应该是std::cout@StoryTeller UnslanderMonica呃,我的错误,看起来我还没有从给出的答案中学到东西!我将编辑我的帖子。noexcept noexcept可能会抛出供您编辑,什么是不工作?我的意思是检查函数将声明为noexcept,但它实际上抛出,因为它调用了将抛出的ASDF。您的打印检查有缺陷。应该是std::cout@StoryTeller UnslanderMonica呃,我的错误,看起来我还没有从给出的答案中学到东西!我将编辑我的帖子。。