c++ - 如何强调该功能可能会抛出?
问题描述
考虑以下函数:
void checkPlayerCounter() {
if (playerCounter != consts::numberOfPlayers) {
throw std::runtime_error("Detected " + std::to_string(playerCounter)
+ " player instead of " + std::to_string(consts::numberOfPlayers));
}
}
该函数可能会抛出,为了便于阅读,我想在头文件内的函数声明中强调这一点。我怎样才能做到这一点?
我正在寻找类似的东西:
void checkPlayerCounter() may throw;
解决方案
您可以noexcept(false)
显式使用。就在你建议的地方:
void checkPlayerCounter() noexcept(false);
不幸的是,没有办法避免双重否定noexcept
。另一种选择是评论:
void checkPlayerCounter(); // may throw
也就是说,除非另有说明,否则任何阅读声明的人都应该始终假定函数可能会抛出异常。
推荐阅读
- ruby-on-rails - 如何从rails中的不同表传递id?
- python - 如何使用 Pyautogui 输入特殊字母
- git - TortoiseGit 从所有分支中拉出不考虑远程分支
- python - Python:找到最大值
- c# - 使用 WCF 打开或呈现局部视图
- python - 如何增加 OpenCV、tkinter 和枕头的 FPS
- node.js - 多个 API 使用节点 js 生成一个数组/对象
- php - PHP 7.0 到 7.2 更新到 7.4
- c - 在C中将数组转换为矩阵
- javascript - 使用 JS 的无限滚动无法在 Chrome 浏览器上运行 - PURE JS