Noexcept函数是否仍可以调用在C ++ 17中引发的函数? [英] Can a noexcept function still call a function that throws in C++17?

查看:91
本文介绍了Noexcept函数是否仍可以调用在C ++ 17中引发的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

P0012R1 使异常规范成为类型系统的一部分

我看到 noexcept 现在已成为函数的一部分类型。

In P0012R1, "Make exception specifications be part of the type system",
I see that noexcept is now becoming a part of the function type.

我无法确定这是否会阻止 noexcept(true)函数仍然能够调用 noexcept(false)函数。

I can't tell whether this will prevent noexcept(true) functions from still being able to call noexcept(false) functions.

下面的代码对C ++ 17仍然有效吗?

Will the following code still be valid for C++17?

void will_throw() noexcept(false){
  throw 0;
}

void will_not_throw() noexcept(true){
  will_throw();
}


推荐答案

根据 cppreference


请注意,对函数的 noexcept 规范不是编译时的
检查;它只是程序员告知编译器
函数是否应该抛出异常的一种方法。

Note that a noexcept specification on a function is not a compile-time check; it is merely a method for a programmer to inform the compiler whether or not a function should throw exceptions.

您的代码的语法有效,但是 std :: terminate 在执行时将被调用。

So the syntax of your code is valid, but std::terminate will be called when executed.

这篇关于Noexcept函数是否仍可以调用在C ++ 17中引发的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆