跟进递归无例外规范的问题。如果我在类或结构的作用域中声明函数f,它应该在类/结构范围内随处可见。也是在not (说明符)中(如果它是递归调用的话,应该不重要)。MSVC v19.28和Clang12.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...))) {
~^~~~~~~发布于 2021-09-07 19:44:40
我相信这是GCC 8中引入的GCC错误。正如你所看到的,当用这里 7.5编译时,所有的东西都编译成功了。
为什么要成功编译?
因为函数模板的专门化的noexcept-specifier只在需要时被实例化。
https://stackoverflow.com/questions/69070187
复制相似问题