decltype(void()) 中的 void() 到底是什么意思?

What does the void() in decltype(void()) mean exactly?

这是 question, more precisely of the comments of 回答的后续。

decltype(void())中的void()到底代表什么?
它代表函数类型、表达式还是其他什么?

我引用了@JoachimPileborg 的 似乎解释正确:

I think I figured it out now, decltype needs an expression, and not a type. void() is not actually a type here, but an expression, a C-style cast (just like e.g. int(12.34)) void(void) is not an expression therefore it doesn't work. How the compiler parses different things depends on the context, when it expects a type it parses as a type, when it expects an expression it parses as an expression. sizeof() (with the parentheses) expects first of all a type, otherwise it's parsed as a parenthesized expression.

我不是在寻找学分或声誉。
无论如何,我想这是一个有趣的答案,值得为未来的读者专门提问。

使用hyperlinked C++ grammardecltype(void())的解析为:

decltype( expression )
decltype( assignment-expression )
decltype( conditional-expression )

...这里有很多涉及操作顺序的步骤...

decltype( postfix-expression )
decltype( simple-type-specifier ( expression-listopt ) )
decltype( void() )

所以void()是一种expression here, in particular a postfix-expression

具体来说,引用 2011 ISO C++ 标准的第 5.2.3 节 [expr.type.conf] 第 2 段:

The expression T(), where T is a simple-type-specifier or typename-specifier for a non-array complete object type or the (possibly cv-qualified) void type, creates a prvalue of the specified type, which is value-initialized (8.5; no initialization is done for the void() case).

因此 void() 是类型 void 的表达式,正如 int() 是类型 int 的表达式(值为 0)。显然 void 表达式没有值,但这里它是 decltype 的操作数,所以它没有被计算。 decltype 仅指其操作数的类型,而不是其值。

decltype(void()) 只是引用类型 void.

的一种冗长方式