为什么 catch 异常声明允许尾随括号?

Why does a catch exception-declaration allow trailing parentheses?

我遇到过一些如下所示的 C++ 代码:

class exception {};

int main()
{
    try {
        throw exception();
    } catch (exception()) {
        // ...
    }
}

请注意 catch (exception()) 中额外的一组括号。根据 Compiler Explorer,这被编译为相同的目标代码,就好像它是用 catch (exception &).

编写的一样

允许额外的一组括号的依据是什么,标准的哪一部分允许这样做?据我所知,catch 子句需要类型说明符,但 exception() 似乎不像类型说明符。

异常处理程序声明像函数声明一样工作,数组和函数类型参数被调整为指针。 (也就是说,不能抛出或捕获数组和函数 "by value"。)具体来说,[except.handle]p2 表示:

A handler of type “array of T” or function type T is adjusted to be of type “pointer to T”.

所以 catch (exception()) 等同于 catch (exception(*p)())

exception() 正在声明一个函数。 Per except#nt:handler, an exception-declaration contains a declarator. A function declaration 是一种声明符。很容易看出这一点,因为 exception(int) 也可以正常工作。请注意,exception-declaration 恰好与 parameter-declaration 完全相同,因此它的工作方式与声明完全相同参数中的函数。