c++ - 类/结构内的递归 noexcept 规范
问题描述
跟进问题Recursive noexcept specification。如果我在类或结构范围内声明函数f,它应该在类/结构范围内的任何地方都可见。同样在 noexcept 说明符中(如果它是递归调用则无关紧要)。MSVC v19.28 和 Clang 12.0.1 接受并编译此代码,但 GCC 11.2 不接受?为什么?它是 GCC 编译器还是 MSVC 和 Clang 中的错误?
struct S {
template<typename T>
static auto f(T && t) noexcept {
return true;
}
template<typename T, typename... Ts>
static auto f(T && t, Ts && ... ts) noexcept(noexcept(f(ts...))) {
return f(ts...);
}
};
int main() {
S::f(true, 0, 5u);
}
GCC 错误信息:
In instantiation of 'static auto S::f(T&&, Ts&& ...) [with T = bool; Ts
= {int, unsigned int}]':
error: no matching function for call to 'S::f(int&, unsigned int&)'
static auto f(T && t, Ts && ... ts) noexcept(noexcept(f(ts...))) {
~^~~~~~~
note: candidate: 'template<class T> static auto S::f(T&&)'
static auto f(T && t) noexcept {
^
note: template argument deduction/substitution failed:
note: candidate expects 1 argument, 2 provided
static auto f(T && t, Ts && ... ts) noexcept(noexcept(f(ts...))) {
~^~~~~~~
解决方案
我相信这是 GCC 8 中引入的一个 GCC 错误。正如您在此处看到的,使用 GCC 7.5 编译时,一切都编译成功。
为什么要编译成功?
因为noexcept
仅在需要时才实例化函数模板特化的 - 说明符。
推荐阅读
- java - 我检查网站是否在线的代码有什么问题?
- php - 错误 - 预期的内容类型 text/json 得到 text/html
- php - 我如何解决upload_max_filesize 问题
- arrays - 如何在 PostgreSQL 中创建具有范围的数组
- javascript - 为什么在 React-Native 中打开 Screen 时总是默认打开 Modal?
- c - 将字符串添加到字符串数组时打印 ����
- java - 为什么我在调用 Exchange Web 服务 API 的 autoDiscoverUrl 方法时遇到延迟?
- optimization - Google 优化报告不显示转化
- ubuntu - 使用“代码”在匹配的 CWD 中打开 VS 代码?
- oracle-data-integrator - 在 ODI 12c 元数据存储库的 SNP_SESSIONS 表中,列 NB_ROW、NB_INS、NB_UPD、NB_DEL 均为 0