c++ - noexcept 函数仍然可以调用在 C++17 中抛出的函数吗?

标签 c++ exception function-pointers c++17 noexcept

P0012R1 , "使异常规范成为类型系统的一部分",
我看到 noexcept 现在正在成为函数类型的一部分。

我不知道这是否会阻止 noexcept(true) 函数仍然能够调用 noexcept(false) 函数。

以下代码对 C++17 是否仍然有效?

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

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

最佳答案

根据 cppreference :

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执行时会被调用。

关于c++ - noexcept 函数仍然可以调用在 C++17 中抛出的函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39763401/

相关文章:

c# - 请求失败,HTTP 状态为 417 : Expectation Failed - Using Web Services

c++ - 收到错误 : no match for operator[] in

c++ - 从 Matlab 连接到本地服务器

javascript 错误和异常处理程序

c++ - 多重分派(dispatch)如何违反 C++ 中的封装

c++ - 子系统:native中的异常处理

c++ - 带函数指针的模板推导还原

ruby-on-rails - 通过与 :method and :method as function arguments in ruby 之间的区别

c++ - 使用 OpenCV C++ 进行楼梯检测的线拟合

c++ - 后缀递增和递减是从左到右关联还是相反?